The function that I believe you are looking for is GetPlayers(), from the Players service. This function returns an array of players that are currently connected to the server. There is also another thing I would like to clear up: You do not need to prefer ipairs(). It is understandable that ipairs() is made for arrays, but pairs() itself also works for arrays. If you have a personal preference, so be it. I will not stop you.
Here is an iteration example of GetPlayers():
local plrs = game:GetService("Players"):GetPlayers()
for i, player in pairs(plrs) do
print(player.Name)
end
Because this is an array, you can also achieve this using a numerical for loop if desired:
local plrs = game:GetService("Players"):GetPlayers()
for i = 1, #plrs do
print(plrs[i].Name)
end
Notice how the generic loop is more straightforward. Arrays start at 1 in Lua and the value within the index is not immediately clear. As player would refer to the player in the current iteration i in the pairs() iterator function, plrs[i] also refers to the player in the current iteration i but it is not nearly as straightforward as player because player directly refers to the value the iteration hits, but plrs[i] refers to the value under index i.
3 comments
does game:GetService("Players"):GetChildren() works same as :GetPlayers()?
Not all of the time. GetChildren() returns every Instance under the object it is called from. GetPlayers() explicitly returns players under the Players service.
Tsym, i'll try that im not sure how i can make this but i think ican make this. Thanks for your answer. You opened new way for me.