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

Help with checking to see how many players in game?

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.

2 comments

0

Is there any errors?

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.

2 answers

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.

2 comments

0

Waiting for PlayerAdded signal would be better rather than waiting.

0

How would I go about putting this into the script? I put >=2 then print bla bla but that didn't work.

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

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