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 5 years ago
Edited 5 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:

01numbers = game.Workspace.Numbers
02value = numbers.PlayerCount.Value
03 
04game:GetService("Players").PlayerAdded:Connect(function()
05    numbers.PlayerCount.Value = numbers.PlayerCount.Value + 1
06    wait(1)
07    print(value)
08end)
09 
10game:GetService("Players").PlayerRemoving:Connect(function()
11    numbers.PlayerCount.Value = numbers.PlayerCount.Value - 1
12    wait(1)
13    print(value)
14end)

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 — 5y
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 — 5y

1 answer

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

yes, its ez bro

1numbers = game.Workspace.numbers --Value
2local players = game:GetService("Players") --Get the service
3local 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
4 
5print(#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 — 5y
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 — 5y
0
nah only if there is something in player list than only players BielNS 40 — 5y
0
nah only if there is something in player list than only players BielNS 40 — 5y
Ad

Answer this question