Hi, I’m trying to create round system and I need to be able to loop through the players currently in the server to access their StarterCharacterScripts. I was wondering how would I create this table.
You can get all players currently in the server using the following code.
local Players = game:GetService("Players")
-- Whenever you need them, call this.
local players = Players:GetPlayers()
-- You can also keep the returned table reference and use event listeners to
-- avoid calling GetPlayers constantly.
Players.PlayerAdded:Connect(function(player) table.insert(players, player) end)
Players.PlayerRemoving:Connect(function(player) table.remove(table.find(players, player)) end)
Please note that you SHOULD NOT EVER leave a Player
reference in a table, as Instance
s are never cleaned up if referenced by your code, even in a weak table. This is why we use PlayerRemoving
.
What is the ‘player’? Is it the table name?
It is a variable given to the person who the event affects e.g. if you joined the game, you would be the player
Here’s what @Sepruko said (with explanation).
local Players = game:GetService("Players") -- Getting the "Players" service
-- Whenever you need them, call this.
local players = Players:GetPlayers() -- This is a method (function) that returns a table of ALL players in the game when it's called.
-- You can also keep the returned table reference and use event listeners to
-- avoid calling GetPlayers constantly.
Players.PlayerAdded:Connect(function(player)
--[=[
This is the `PlayerAdded` event, it fires when a user joins the game
and you can connect it to a function, that function will have one
parameter, the player's Instance (The object you can see in the explorer
tab under `Players`).
]=]
table.insert(players, player)
--[=[
Here we are inserting the player who just got joined the game
to the table since he wasn't there when we called the function
hence wasn't in the table.
]=]
end)
Players.PlayerRemoving:Connect(function(player)
--[=[
This is the `PlayerRemoving` event, it fires when a user leaves the game
and you can connect it to a function, that function will have one
parameter, the player's Instance (The object you can see in the explorer
tab under `Players`, it won't be there tho as he left).
]=]
table.remove(table.find(players, player))
--[=[
Here we are removing the player who just got left the game
from the table since Roblox doesn't remove him automatically,
this is done to avoid players being in the table while not being in
the game.
]=]
end)
Got it, but how would I call on the table. Like if I wanted to print whatever is on the index position 1. What variable/table would go in the print(***[1])
players
..
As @msix29 said, the table with all the players is called players
so the first player would be players[1]
and so forth
I believe you can just
local TABLE
if #game.Players:GetPlayers() >= 0 then
TABLE = game.Players:GetPlayers()
end
Thanks, just to be 100% sure, would I be able to loop through the player characters by doing .character?
Why would you do this when var = game.Players:GetPlayers()
would return nil if there were no players?
That is correct, Player.Character would work
Does game.Players:GetPlayers()
return nil if there are no players? I’m pretty sure it just returns an empty {}
.
I don’t see why the if
is even needed. Just repeat game.Players:GetPlayers()
every time you need it. (except that you should still do game:GetService("Players")
)
Off topic but i dont mind the extra check even if not needed
Apologies, you are correct, it doesn’t return nil, however I still don’t see why a check would be needed in this use as if the table is empty, it simply won’t iterate through
I see no benefit in this, is there a reason you do?
Let’s not make this post off topic, right?
It was completely on topic, what are you on about? It was a genuine criticism with the code that was worth changing.