Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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!

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 EpicMetatableMoment 1444 — 4y
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 guywithapoo 81 — 4y

1 answer

Log in to vote
1
Answered by
BielNS 40
4 years ago

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

2
players:GetPlayers() better because GetChildren() will reaturn a table of all children of players, and GetPlayers() will return a table with only players. Lazarix9 245 — 4y
1
and also this will get player count noly once so its better to use with a players.PlayerAdded listener or whatever its called Lazarix9 245 — 4y
0
nah only if there is something in player list than only players BielNS 40 — 4y
0
nah only if there is something in player list than only players BielNS 40 — 4y
Ad

Answer this question