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

Script skips line where it checks if there are a certain amoutn of players?

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

2 comments

0

Is it printing anything? also, something i would do and you should try is do Players:GetChildren() instead of Players:GetPlayers()

0

I think the reason it isnt working because all youre doing is just doing a print statement, what you could do isafter it is: elseif #game.Players:GetPlayers() > 2 then [your code here] end since you dont have anything checking if its more than 2 players

2 answers

✓ Accepted answer

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 loop method

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 loop method

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.

1 comment

0

nooooooooooooooooooo please use PlayerAdded:Wait(), not repeat wait until ...                                       please please please

local RunService = game:GetService('RunService')

while #game.Players:GetPlayers() < 2 do
	RunService.Heartbeat:Wait()
end

1 comment

0

This isn't a very good way to approach this problem. An explanation would also be nice.

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