Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Created August 25, 2020 12:04
Show Gist options
  • Save cxmeel/5f0a796b072bb199c81569f842bb6c50 to your computer and use it in GitHub Desktop.
Save cxmeel/5f0a796b072bb199c81569f842bb6c50 to your computer and use it in GitHub Desktop.
Fire RemoteEvents to selected Clients
local Module = {}
local Players = game:GetService("Players")
--[[
ResolvePlayers(
PlayerList: Player | Player[] | string | string[] | number | number[]
): table
Converts an array of values (or single value) into an Array of Players.
--]]
local function ResolvePlayers(PlayerList)
local Players = Players:GetPlayers()
--[[
If PlayerList is not a table, convert the PlayerList variable
into a table, and insert the current value of PlayerList into
the new table.
--]]
if type(PlayerList) ~= "table" then
PlayerList = { PlayerList }
end
--[[
Iterate through each value of the PlayerList table and resolve
each value to a matching Player Instance.
--]]
for Key, Value in next, PlayerList do
--[[
If Value is already a Player, skip to the next Value in the
PlayerList table.
--]]
if type(Value) == "userdata" and Value:IsA("Player") then
continue
end
--[[
If Value is a string, find a Player whose name matches the
given string. This will perform a case-insensitive match by
converting the Player's Name and the given string to lowercase.
--]]
if type(Value) == "string" then
for _, Player in next, Players do
if Player.Name:lower() == Value:lower() then
PlayerList[Key] = Player
--[[
We found a match, break out of the loop and
check the next Value.
--]]
break
end
end
--[[
If Value is a number, find a Player with a matching UserId.
--]]
elseif type(Value) == "number" then
for _, Player in next, Players do
if Player.UserId == Value then
PlayerList[Key] = Player
break
end
end
--[[
If the Value isn't a Player, number or string, we don't know
how we should process it, so just throw an error and break out
of the script.
--]]
else
error(string.format("Expected Player to be of type Player, string, number; got %s", typeof(Value))
end
end
--[[
We successfully resolved all Values to Player Instances, so pass the
table of Players back to the caller.
--]]
return PlayerList
end
--[[
Module.FireClients(
Remote: RemoteEvent,
PlayerList: Player | Player[] | string | string[] | number | number[],
...rest: any
): void
Fires a RemoteEvent to selected clients. Clients can be specified as a single
Player, string or number, or a mixed Array of Players, strings and numbers.
Example:
Module.FireClients(
ReplicatedStorage.MyRemoteEvent,
{
Players.Player1, --> Resolves to: Players.Player1
"Player 3", --> Resolves to: Players.Player3
123456 --> Resolves to: Players.Player4
},
"Hello world!"
)
Will fire "MyRemoteEvent" on Player1, Player2 and Player4 with the
value "Hello world!"
--]]
function Module.FireClients(Remote, PlayerList, ...)
--[[
Check that the given Remote is a RemoteEvent. If not, throw an
error.
--]]
assert(
type(Remote) == "userdata" and Remote.ClassName == "RemoteEvent",
string.format("Expected Remote to be a RemoteEvent; got %s", Remote.ClassName
)
--[[
Call the ResolvePlayers method using the given PlayerList and
iterate through each Player, calling Remote.FireClient on each
Player, with the data given as the additional arguments.
--]]
for _, Player in next, ResolvePlayers(PlayerList) do
Remote:FireClient(Player, ...)
end
end
--[[
Export the module.
--]]
return Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment