Getting all Player's UserId In a table

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.

1 Like

Hello, Can you try and use GetChildren?

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?

userIds[player.UserId] = player.Name
1 Like

That looks fine. Is it running at the start of the cutscene? Are you checking the server output to see if it prints instead of the client output?

1 Like

am checking if the server output prints.

1 Like

You could probably run the cutscene on client, but it just depends what you are actually doing in the cutscene and if you need it to be server.

1 Like

The cutscene is for everyone to see so i decided to put it on server script, will is still be the same if I put it on the client?

1 Like

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

1 Like

I put a wait command before my block of script and it worked lol

1 Like
local userIds = {}
local players = game:GetService("Players")

for _,player in pairs(players:GetPlayers()) do
     table.insert(userIds,player.UserId)
end

I suggest using GetPlayers and not GetChildren when working with players specifically.

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

1 Like