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

If statement error in a while loop (Game Development)?

Hello, I have been experiencing a problem with this for a while now. This is pretty much part of a main script for a game that is being made.

So what is happening is that whenever the timer hits 0 it will end the round and choose weather there was a winner or there were no winners.

The problem is that it will not end early if there are less than one or one player active; it will just wait until the timer hits 0.

A small portion of the code is below (Has the if statements to end the round): There are no errors in the outputs, this leads me to think it is a logic issue or something.

while true do
		wait()
		while LocalTimer > 0 do
		wait(1)
		LocalTimer = LocalTimer - 1 
		TimerTag.Value = LocalTimer 
		
		ActiveContestants = {}
		for _, player in pairs(Contestants) do
			if player then 
				local Character = player.Character
				if Character then 
					local MatchTag = Character:FindFirstChild("MatchTag")
					local Humanoid = Character:FindFirstChild("Humanoid")
					if MatchTag and Humanoid and Humanoid.Health > 0 then
						table.insert(ActiveContestants, player)
					end
				end
			end
		end
		end
		
		if TimerTag.Value == 0 then
			if #ActiveContestants >= 2 then
				gameresult = "End"
				break
			elseif #ActiveContestants == 1 then
				gameresult = "End"
				break
			end
		end
		
		if #ActiveContestants <= 1 then
			gameresult = "End"
			break
		end

		if gameresult == "End" then 
			if #ActiveContestants >= 2 then
				Event:FireAllClients("NobodyWon")
			elseif #ActiveContestants == 1 then
				Event:FireAllClients("OnePlayerWon")
			end
		end
	end
	end
	
if gameresult == "End" then
	print "Game has ended... Restarting the game..."
	gameresult = "Intermission"
end

Thanks if you can help in advance.

1 answer

Break breaks out of the loop instantly. That last if statement in the inner while loop will never get reached as it's at the bottom - and the condition being set is always followed immediately by a break.

1 comment

0

So I should move it up?

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