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!
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
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.