While loop is ignoring the condition needed for it stop the loop

Hello,
So I am building a round system game.
The round ends whenever the time has run out OR when all players have died.
This sounds really simple and it probably is but I can’t get it to work no matter what.

This is my current code

while playerCount.Value > 0 do
		if timePassed <= TimeLimit then
			--Time left is equal to itself minus the time that has passed.
			local TimeLeft = TimeLimit-timePassed
			--Sets the status value with the remaining time.
			status.Value = "You have "..TimeLeft.." seconds left to escape!"
			--Add 1 more second to the time that has passed.
			timePassed = timePassed+1
			wait(1)
		end
	end

The variable “playerCount.Value” holds the number of players alive, so while that number is above 0, this should keep looping. The if statement just checks if the time has run out. Thing is, whenever I die or manually set the player count’s value to 0 or lower, it won’t stop the loop. I have tried doing:

while timePassed <= TimeLimit and playerCount.Value > 0

The problem with that one is that it will only stop whenever BOTH conditions have been done, and that’s what and does, so when I try to convert it to or like this…:

while timePassed <= TimeLimit or playerCount.Value > 0

That will keep looping until either the time has run out or the player count is lower than 0, but it still won’t work and I don’t understand why.

I have tried not() and also repeat until and every combination possible but it just won’t update. I have even tried making prints to see if it even reaches certain points on the code but it just won’t ever run the playerCount. I have triple checked that the playerCount is correctly hooked up to the INT variable. It all should work but strangely enough, it won’t.

Any help would be appreciated.
Thanks in advance!

1 Like

Is it getting stuck with a value that isn’t quite 0 due to floating decimal points?

How about while not playerCount.Value < 1

You want to use this statement because the loop will end when either one of the conditions are false, which is what you want.

1 Like

I don’t think that would make a change because player count value can’t have decimal points.

Just like @Arszm said, it never has a decimal value, but just in case I added a <= 0

Did try that, but it won’t stop the loop. It does stop when the time has run out but it does not stop whenever the plaeyr count reaches 0 (tried negative numbers as well just to see if that did anything but didn’t)

change > to a => and then it will stop when it reaches 0.
> means greater than. => means greater than or equal to.

1 Like

Are you sure playerCount even reaches 0? Does the system for changing player count even work?

Already did, didn’t work.
I also tried it both ways as in <= and >=

I think I found the mistake but still don’t know how to fix it.
the playerCount does reach 0, and the system does work, but this is where I believe the problem lies, this is the entire function:

function gameTimer()
	playerCount.Value = MatchStats.totalCount.Value
	--Enable matchInProgress so no one else can join
	MatchStats.matchInProgress.Value=true
	--Variable that stores the time that has passed.
	local timePassed = 0
	--Sets the status value.
	status.Value = "Escape the rooms before the timer runs out!"
	--Repeat until the ammount of time that has passed is less than the time limit.
	
	while timePassed <= TimeLimit and playerCount.Value > 0 do
			--Time left is equal to itself minus the time that has passed.
			local TimeLeft = TimeLimit-timePassed
			--Sets the status value with the remaining time.
			status.Value = "You have "..TimeLeft.." seconds left to escape!"
			--Add 1 more second to the time that has passed.
			timePassed = timePassed+1
			wait(1)
			if playerCount.Value <= 0 then
				print(playerCount.Value)
			end
			if playerCount.Value >= 0 then
				print(playerCount.Value)
			end
	end
end

So I have two variables that store the number of players. one is totalCount that stores the total amount of players currently online, and there’s playerCount, that only stores the number of players when the round started, but I think it’s binding the two of them, but if that were true, then on the int value stored inside the script it would display a different number

Instead of a while loop how about a repeat - until loop?

Already tried that, I tried a repeat, a while, an if, a for, every type of loop and it still won’t work, but I believe it has something to do with the function I posted above.

Do you get any error messages in output?

The function gets called when the round begins.
I typed in those prints at the end just to be sure that the variable DOES get called, but it’s weird because on the part where I can change the variable on runtime it says 0 while on the console it’s printing 1, not 0. So it’s actually printing totalCount and not playerCount

None. Just the prints to verify that the if and variable actually works.

I’m guessing the fix would be to assign the value of totalCount to playerCount and then somehow unlink them so there’s no correlation between the two.

Your code seems right. Maybe you messed something up about playerCount and totalCount in other script.

What I mean by that is this:

if playerCount.Value > 0 then
print("Above 0, Value is: "..playerCount.Value)
end

that in the console gives me this result
Before and after the player dies.

But, in the variable, it says this:

So it doesn’t make sense. It says it has a value of 0 but prints 1

The only other script that uses the variable is the one responsible for substracting 1 from playerCount whenever the player dies/leaves/wins while in a match

Is this a client-server issue? Make sure you’re setting and recording both values on the server.