Ok so hi. Recently i have been making a game like "Giant survival". I have a line of code in my while loopt that checks if there are a ceratin amount of players. But when i test it in studio it skips it and just continues on with the main code.
Code:
while true do if #game.Players:GetPlayers() < 2 then print("Need more players") end -- // Continue here wait() end
I think I got what you are trying to do.
Pause the game while there are not enough players in the server.
Your code is failing because of your use of if
. if
simply reads the value its given and runs code if its true, continues if its not. In order to pause the code until there are enough players you will need a repeating function. You can approach this many different ways.
repeat print("Need more players") wait() until #game.Players:GetPlayers() > 2
This requires a check beforehand if you don't want to have it print Need more players
if there are already enough players. So we will need an if statement.
if #game.Players:GetPlayers() < 2 then repeat print("Need more players") wait() until #game.Players:GetPlayers() > 2 end
This method is starting to get pretty bulky, repeat is better utilized for "pause this code until" situations with short wait times, luckily there are other methods.
while #game.Players:GetPlayers() < 2 do print("Need more players") wait() end
That's it for this method. I'd recommend going with it.
There are other methods but these should be good enough to get you started. Hope this helped.