How to make a script that waits for more players before starting the game?

Hi, I was wondering how to make a script that waits for more players to join before starting the game.

I know this is a very simple thing to script but I am quite new to scripting so cut me some slack will ya?

My plan for the game I’m making is to make a round system, but I need to have more than 1 player for a round to start because the game is a multiplayer game.

I have looked up in Google for any videos or tutorials for “how to make a script that waits for players” but I found nothing. I also searched in the Roblox Wiki but I didn’t find anything there either. Thanks!

14 Likes

You could use the :GetPlayers function of player service. It returns a table, then you could count how many players are in the game

--Get player service
local players = game:GetService(“Players”)

--Check player count, run stuff if it’s high enough
while wait() do
    if #players:GetPlayers() > 10 then -- Change this number to whatever you want
        print(“Enough players!”)
    else
        print(“Not enough”) --Change this line to whatever you want like changing a GUI
    end
end
19 Likes

It works, thanks a lot!

I don’t know what else to say I just need to get to 30 characters.

2 Likes

Here is another solution, which avoids polling.

local Players = game:GetService("Players")

while #Players:GetPlayers() < 10 do
	Players.PlayerAdded:Wait()
end
14 Likes

instead of that u can use

    local players = game:GetService(“Players”)
    repeat wait() until  #players:GetPlayers() > 10
3 Likes

yeah but that uses polling which kinda wastes resources The second solution is better.

1 Like

its basically the same thing if you dont want waste resources then add wait(1) or 2 because we can only use a loop to detect that

I’d wager that they actually aren’t and the second solution is indeed better.

The second solution does use a terminating loop but it’s event-based as it waits for PlayerAdded to fire before starting its next iteration. This is different from polling; instead of checking the player count after the event fires, you’re yielding the thread if the condition isn’t met.

Repeat and a while equivalent of your suggestion are both bad. Forget about how bad wait can get, the repeat loop can end up waiting more time than is actually necessary. The conditional of the second solution is only checked when a new player is added rather than periodically.

Increasing the wait time exacerbates the problem of waiting more time than is actually necessary and doesn’t solve the root issue. You should avoid polling or using loops if a better event-driven solution is presented to you.

1 Like

Why are we replying to my super old posts now? I mean go ahead and have a conversation if you want, I’m not stopping you.

4 Likes