Making a game where only a certain amount of players can finish each round so there might be 20 players in the server but on 15 can move on to the next round. I made it so right now after the round is finished it makes a table of the list of player names that finished and should move on to the next round. I had to pass it through a remote event which is why I used player names instead of the player instance. I made another table in the next script that makes a table of all of the PLAYER INSTANCES in the server. How can I compare both tables to make one table with the right player instances using the table with player names.
for _, Plr in next, Plrs do
end
This is all I have so far. “Plrs” is the table with all of the players in the server and the table with the player names is called “FinishedPlayersNames”
Player objects can be passed through RemoteEvents just fine.
--Server
game.Players.PlayerAdded:Connect(function(player)
game.ReplicatedStorage.RemoteEvent:FireAllClients(game.Players:GetPlayers())
end)
--Client
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(players)
for _, p in ipairs(players) do
print(p)
end
end)
Here’s an answer though:
local players = {}
for _, playerName in ipairs(playerNames) do
local player = game.Players:FindFirstChild(playerName) --You could argue that you should call Players:GetPlayers() and do a search though it every time, because non-player Instances can be childrenf of Players. It's fine to just choose to never do that though, there's never any good reason to IMO
if not player then continue end --Must check because player might have left since playerNames was generated
table.insert(players, player)
end
You can pass a table of player instances through a RemoteEvent/RemoteFunction instance without issue. However, to achieve what you’re describing you’d do the following.
--SERVER
remote:FireClient(player, playerNames) --"player" is the client being fired, "playerNames" is a table of player names.
--CLIENT
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
remote.OnClientEvent:Connect(function(playerNames) --Table of player names received by the client.
local playersTable = {} --Table to store player instances.
for _, playerName in ipairs(playerNames) do
local player = players:FindFirstChild(playerName)
if player then
table.insert(playersTable, player)
end
end
--Do stuff with the table of player instances.
end)