Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is this an efficient way to get the number of players in a game?

Hello, I have created a script that has a NumberValue in a Folder inside of Workspace. Whenever somebody joins, it adds + 1 to the NumberValue. Whenever someone leaves, it does - 1. I am just curious to know if the way I am doing this is efficient because I know there is other ways of doing this. I also forgot to mention that whenever someone joins it just prints out 0 for some reason. Thanks!

Code:

numbers = game.Workspace.Numbers
value = numbers.PlayerCount.Value

game:GetService("Players").PlayerAdded:Connect(function()
	numbers.PlayerCount.Value = numbers.PlayerCount.Value + 1
	wait(1)
	print(value)
end)

game:GetService("Players").PlayerRemoving:Connect(function()
	numbers.PlayerCount.Value = numbers.PlayerCount.Value - 1
	wait(1)
	print(value)
end)

Thank You!

2 comments

0

Please don't post questions that have been asked before. The answer you're looking for can be found here: https://scriptinghelpers.org/questions/85605/how-to-see-how-many-players-there-are-in-your-server

0

I was only curious to know if the method i was using was as good as GetPlayers, it dont think its the same thing

1 answer

✓ Accepted answer

yes, its ez bro

numbers = game.Workspace.numbers --Value
local players = game:GetService("Players") --Get the service
local child = players:GetChildren() --Get every player in the list, this returns an array, but can also be used with #, example #child, to get the exact number

print(#child)

if this helped then please mark this as the answer

4 comments

2

players:GetPlayers() better because GetChildren() will reaturn a table of all children of players, and GetPlayers() will return a table with only players.

1

and also this will get player count noly once so its better to use with a players.PlayerAdded listener or whatever its called

0

nah only if there is something in player list than only players

0

nah only if there is something in player list than only players

This discussion is preserved as it was. New answers, comments, and votes are closed.