Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Getting current player count?

Asked by
J_98482 18
7 months ago

I've tried a bunch of ideas to give my code an accurate player count, but so far, nothing has worked out. The piece of code that's gotten me closest looks something like this - 'val' being a number variable that stores the number of players. I've set it up so that it checks every .15 seconds.

while true do
    wait(0.15)
    game.Players.PlayerAdded(function(player)
    val.Value = val.Value + 1
end)
game.Players.PlayerRemoving(function(player)
    val.Value = val.Value - 1
end
end)

This code works in simple tests, but on a real server, it ends up adding/removing over 100 to the value instead of just one. Is there a more simple way I could get the number of players currently on the server. Any answers are greatly appreciated.

3 answers

Log in to vote
1
Answered by 7 months ago
Edited 7 months ago

You can use the :GetChildren() function and the # operator to get how much players are ingame. The # returns the length of a table and :GetChildren() and :GetPlayers() returns a array, which is a table.

local players = game.Players:GetPlayers() -- getting players

val.Value = #players -- the value will show the length of the table
Ad
Log in to vote
0
Answered by
RayCurse 1348 Moderation Voter
7 months ago
Edited 7 months ago

If you want to get player count, just do this:

local playerCount = #game.Players:GetPlayers()

GetPlayers() is the method of the Players service and returns a table containing all the Player objects. The # (length operator) is used to get the length of the table returned.

Next time you have a problem like this, I suggest you search up the problem yourself as simple things like these often have simple solutions. Nonetheless, I will show you what you were doing wrong in your method (for the sake of learning).

The problem with your method is that while loops aren't needed here. I suggest you read up on RBXScriptSignals. Events fire whenever a particular occurrence happens in the game (in this case a player joining/leaving). There is only need to connect them once. Here's the correct code:

game.Players.PlayerAdded:Connect(function(player)
    val.Value = val.Value + 1
end)

game.Players.PlayerRemoving:Connect(function(player)
    val.Value = val.Value - 1
end)
1
You should probably explain why your code works (:GetPlayers() returns a list of players and #list returns the length of a list) User#22604 1 — 7mo
0
I was having trouble with this too DominusInfinitus 235 — 7mo
Log in to vote
0
Answered by 7 months ago
Edited 7 months ago

Easy fix: create a variable, add the variable every time a player is detected...

local players = game.Players:GetChildren() -- list of players
local player_count = 0 -- currently 0 players

while wait(1) do -- updater
    for i, player in pairs(players) do -- loop to get players
        player_count = player_count + 1 -- add every player detected.
        print("There are "..i.." players in the game") -- print how many players
    end
end

This is I guess easy to read but the other questions are better, don't read this it is just another way if those other codes don't work..

Answer this question