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

Help with checking to see how many players in game?

Asked by 3 years ago

So this is what I've got and it isn't working, but it works in a click detector only for some reason?

while true do
    players = game.Players:GetPlayers()
    players = game.Players:GetChildren()
    if #players >= 2 then
    print("Working")
    wait(5)
    end    
end

I want it to print the message when the game has 2 or more players.

0
Is there any errors? User#41156 0 — 3y
0
It should error that script exhaustion time because you only put wait(5) in the if statement, that way if there is less than 2 players it will repeat the loop with speed of light and crash it. imKirda 4491 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I prefer doing this:

repeat wait() until #game:GetService("Players"):GetChildren() >= 2

This is how you do it in your script:

repeat wait() until #game:GetService("Players"):GetChildren() >= 2
print("Kitty roger, 123")
-- rest of your code

The repeat... until isn't a code block like if... then so it goes on one line.

0
Waiting for PlayerAdded signal would be better rather than waiting. imKirda 4491 — 3y
0
How would I go about putting this into the script? I put >=2 then print bla bla but that didn't work. NortonHDD 21 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

What I did was set it as a value that adds and removes when player joins.

local val = game.ReplicatedStorage.Playercount
while true do
    game.Players.PlayerAdded:Connect(function(player)
        val.Value = val.Value + 1
    end)

    game.Players.PlayerRemoving:Connect(function(player)
        val.Value = val.Value - 1
    end)
    wait()
end

Answer this question