Timer going faster and faster

Hi! im having an issue with a timer.
I have an obby where you have 4 minutes to beat it. for this im using this function

local function UpdateTimer(TimerLabel,deltaTime)
	timeRemaining -= deltaTime

	local m = math.floor(timeRemaining/60) -- Minutes
	local s = math.floor(timeRemaining%60) -- Seconds -- Milliseconds
	
	if TimerLabel then
		TimerLabel.Text = string.format("%i:%02i", m, s)
	end
end

and this is what happens when you enter the area this is what happens

if part.Name == "ContentDeletedObby" then
	timeRemaining = 240
	char.Humanoid.WalkSpeed = 16
	player:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui").Enabled = false
	local GlitchedClone = script:WaitForChild("Glitched"):Clone()
	GlitchedClone.Parent = player:WaitForChild("PlayerGui")
	
	while timeRemaining > 0 do
		UpdateTimer(GlitchedClone:FindFirstChild("TextLabel"),task.wait())
	end
	
	char:MoveTo(workspace.Spawns.DefaultSpawnLocation.Position + Vector3.new(0,5,0))
	GlitchedClone:Destroy()
	
end

But if you exit the area, and enter it, (using zoneplus) the timer goes faster.
I have not tried any solutions as I don’t know much about it.

video:

2 Likes

It’s because you keep touching the part, which calls the function again.
Try adding a debounce, so that it can only trigger once.

1 Like

Try to add a debounce like @lolbiIlyyy said:


local debounce = false
if part.Name == "ContentDeletedObby" and not debounce then
debounce = true
	timeRemaining = 240
	char.Humanoid.WalkSpeed = 16
	player:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui").Enabled = false
	local GlitchedClone = script:WaitForChild("Glitched"):Clone()
	GlitchedClone.Parent = player:WaitForChild("PlayerGui")
	
	while timeRemaining > 0 do
		UpdateTimer(GlitchedClone:FindFirstChild("TextLabel"),task.wait())
	end
	
	char:MoveTo(workspace.Spawns.DefaultSpawnLocation.Position + Vector3.new(0,5,0))
	GlitchedClone:Destroy()
	
end

I think i discovered a solution, basically when you exit it it sets time remaining to 0, so it stops the loop. but the thing is it will teleport the player to the spawn even if they beat the obby.

note, i managed to just make a global variable if the player beats the obby.

1 Like

pretty sure this is not a jojo reference. anyway can u share the full code

1 Like

If you’re using Zone+ you can use the function Zone:getPlayers() to get the players inside the obby and check if the LocalPlayer is still inside (Assuming you using a LocalScript for this). Something like this could fix the issue probably?

while timeRemaining > 0 do
	local playersInZone = Zone:getPlayers() -- Get the players inside the zone.
	if #playersInZone > 0 and not table.find(playersInZone, player) then -- If there's more than one player in zone and is not the local player.
		break -- Stop the loop
	end
	UpdateTimer(GlitchedClone:FindFirstChild("TextLabel"),task.wait())
end

I don’t usually use Zone+ anymore but I remember it had a function like that.
Since there was no stop for the loop, the loops can stack and make the timer go faster, is like if you do task.wait() * 2 over and over again.

if part.Name == "ContentDeletedObby" then
	timeRemaining = 240
	char.Humanoid.WalkSpeed = 16
	player:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui").Enabled = false
	local GlitchedClone = script:WaitForChild("Glitched"):Clone()
	GlitchedClone.Parent = player:WaitForChild("PlayerGui")
	
	if thread and coroutine.running(thread) then
		task.cancel(thread)
	end
	
	thread = task.spawn(function()
		while timeRemaining > 0 do
			UpdateTimer(GlitchedClone:FindFirstChild("TextLabel"),task.wait())
		end

		char:MoveTo(workspace.Spawns.DefaultSpawnLocation.Position + Vector3.new(0,5,0))
		GlitchedClone:Destroy()
	end)
end