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

How do you check if there is a certaint amount of players?

I have tried this:

if game.Players.NumPlayers >= 4 then
	-- I put my code here...
end

2 answers

✓ Accepted answer

You could put your main script into a function. You could do that like this:

function Round()
	--Code
end

And I assume you would like for there to be 4 players or higher. So, to do that, you could loop your function and check if there were 4 or more players.

while true do
	if game.Players.NumPlayers >= 4 then
		Round()
	else
		break  --Exits the loop and nothing happens
	end
	wait(1) --Always end your infinite loops (while true do) with a wait. Otherwise, studio will crash and you have to start over.
end

5 comments

0

So I put my code under Round?()

0

Yes. And the part below calls the function.

0

It says "Player is not a valid member or Datamodel"

0

..?

0

Oh I haven't said what Round() is yet XD.

Note: Once you add repeat wait() until game:GetService('Players').NumPlayers >= 4, it pauses the rest of the script until there is 4 or more players.

Run code once if there is 4 or more players:

repeat wait() until game:GetService('Players').NumPlayers >= 4
-- your code here

Run once every time a player joins if there is 4 or more players:

game:GetService('Players').PlayerJoined:connect(function(player)
	if game:GetService('Players').NumPlayers >= 4 then
		-- your code here
	end
end)

Run code every 0.03 seconds if there is 4 or more players:

while true do
	if game:GetService('Players').NumPlayers >= 4 then
		-- your code here
	end
	wait()
end

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