I have been making a cutscene which shows the player’s avatar in it, so because of that I want to make a table with all the players’ userId in the server and pick a random UserId to make the dummy look like the chosen userId. I have tried a fairly simple script to get all the players’ userId in a table, which is this:
local userIds = {}
for _, player in pairs(game.Players:GetPlayers()) do
table.insert(userIds, player.UserId)
end
print(userIds)
But when I print it out, it does not show any of the player’s userIds. It works on the local script so I have also tried to put a remote event to transfer the table to a server script as I want my cutscene to be in the server side, but still, the results seems to be the same as last time. Can somebody please suggest me what to do? Should I put my cutscene in the local script instead? Thank you for reading.
local userIds = {}
for _, player in pairs(game.Players:GetChildren()) do
table.insert(userIds, player.UserId)
end
print(userIds)
A little Note you can also do “userIds[UserId] = PlayerName” if you dont want user Id for 2 of the tables
in case you want to get the player name for something?
are you waiting until people actually join? or are you putting it at the beginning of a server script. The only reason I could think of that code not working is if it was ran on startup, before anyone joined
unfortunately, this will not live-update when players get added or removed from the game
this code will tho
local List = {}
function PlayerAdded(plr)
table.insert(List, plr.UserId)
end
function PlayerRemoving(plr)
local Ind = table.find(List, plr.UserId)
if Ind then table.remove(List, Ind) end
end
game.Players.PlayerAdded:Connect(PlayerAdded)--listener for when a player gets added
game.Players.PlayerRemoving:Connect(PlayerRemoving)--listener for when a player gets removed
for i,v in pairs(game.Players:GetPlayers()) do--add all of our current players into the list
PlayerAdded(v)
end
edit: props if you make it a modulescript so all scripts can access it lol