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

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

Asked by 9 years ago

I have tried this:

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

2 answers

Log in to vote
1
Answered by
Relatch 550 Moderation Voter
9 years ago

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
0
So I put my code under Round?() antlerer 33 — 9y
0
Yes. And the part below calls the function. Relatch 550 — 9y
0
It says "Player is not a valid member or Datamodel" antlerer 33 — 9y
0
..? Relatch 550 — 9y
0
Oh I haven't said what Round() is yet XD. antlerer 33 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

Answer this question