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

For loop timer isnt changing when code tells it to change? No error?[SOLVED]

I have a for loop that runs the round timer.

The round can end by 3 things happening

  1. Red team loses all tickets
  2. Blue team loses all tickets
  3. Time runs out

But what if number 1 or 2 happens and there is still time on the clock. Well thats where my problem is occurring.

I have a remote even that fires when either team has 0 tickets and it does fire, but it doesnt change the number in the for loop.

So here is the for loop code, I figured thats all the code you need because it is firing the code but the code just isnt doing anything.

Heres the for loop code.

	-- Starts round timer
	for  i = RoundTime, 0, -1 do
		local function onTimerResetEventFired()
			print("TimerResetFired")
			RoundTime = 3
			print(RoundTime)
		end
			
		Status.Value = "10 MINUTES LEFT"
		if i <= 540 then 
		Status.Value = "9 MINUTES LEFT"
		end
		if i <= 480 then 
		Status.Value = "8 MINUTES LEFT"
		end
		if i <= 420 then 
		Status.Value = "7 MINUTES LEFT"
		end
		if i <= 360 then 
		Status.Value = "6 MINUTES LEFT"
		end
		if i <= 300 then
		Status.Value = "5 MINUTES LEFT"
		end
		if i <= 240 then
		Status.Value = "4 MINUTES LEFT"
		end
		if i <= 180 then
		Status.Value = "3 MINUTES LEFT"
		end
		if i <= 120 then
		Status.Value = "2 MINUTES LEFT"
		end
		if i <= 60 then
		Status.Value = i.." SECONDS LEFT"
		end
		if i == 0 then
			Status.Value = "ROUND OVER!"
			for _, Player in pairs(game.Players:GetChildren()) do
				Player.Backpack:ClearAllChildren()
				Player.Character.Humanoid:UnequipTools()
				StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
				if Player.Character:FindFirstChildOfClass("Tool") then
					Player.Character:FindFirstChildOfClass("Tool"):Destroy()
				end
			end
			wait(3)
		end
		TimerReset.OnServerEvent:connect(onTimerResetEventFired)
		wait(1)
	end

I know its inefficient but anyways whenever the code fires it sets the timer to 3 for a split second then it goes back to its old number where it left off.

Any one have any ideas? Thank you for your time :)

[UPDATE] My current code is this, but it still doesnt work any other ideas?

	TimerReset.OnServerEvent:connect(function()
		TimerReset = true
		print("Fired")
		print(TimerReset)
	end)		
	
	-- Starts round timer
	for  i = RoundTime, 0, -1 do	
		if TimerStop == true then
			break
		end	
		Status.Value = "10 MINUTES LEFT"
		if i <= 540 then 
		Status.Value = "9 MINUTES LEFT"
		end
		if i <= 480 then 
		Status.Value = "8 MINUTES LEFT"
		end
		if i <= 420 then 
		Status.Value = "7 MINUTES LEFT"
		end
		if i <= 360 then 
		Status.Value = "6 MINUTES LEFT"
		end
		if i <= 300 then
		Status.Value = "5 MINUTES LEFT"
		end
		if i <= 240 then
		Status.Value = "4 MINUTES LEFT"
		end
		if i <= 180 then
		Status.Value = "3 MINUTES LEFT"
		end
		if i <= 120 then
		Status.Value = "2 MINUTES LEFT"
		end
		if i <= 60 then
		Status.Value = i.." SECONDS LEFT"
		end
		if i == 0 then
			Status.Value = "ROUND OVER!"
			for _, Player in pairs(game.Players:GetChildren()) do
				EndGameLost:FireClient(Player)
			end
			wait(3)
		end
		wait(1)
	end

1 answer

You can use a break statement to exit out of a for loop when the remote event fires. You can do this by having a boolean value that changes when the remote events. Then make the for loop break when the condition is false. In addition, you can also optimize your timer to take up less lines of code. Here it is:

local timerReset = false
timerReset.OnServerEvent:Connect(function()
	timerReset = true
	--One team has lost all tickets and for loop stopped
end)
for i = RoundTime , 0 , -1 do
	--Exit if timer has been reset
	if timerReset then
		break
	end
	local minutes = math.floor(i/60)
	if minutes > 1 then
		Status.Value = i.." MINUTES LEFT"
	elseif minutes == 1 then
		Status.Value = i.." SECONDS LEFT"
	elseif minutes <= 0 then
		Status.Value = "ROUND OVER"
		for _ , Player in pairs(game.Players:GetPlayers()) do
			Player.Backpack:ClearAllChildren()
			Player.Character.Humanoid.UnequipTools()
			StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
			if Player.Character:FindFirstChildOfClass("Tool") then
				Player.Character:FindFirstChildOfClass("Tool"):Destroy()
			end
		end
		wait(3)
	end	
	wait(1)
end

3 comments

0

How come you dont do, If timerReset == true then break end

0

This is so the for loop timer breaks (exits) when the remote event has fired

0

It doesnt work heres a link to my new code on CodeShare.io https://codeshare.io/aJ6PMK

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