Help with stopping a function when the timer reaches 0

I’m trying to make a script for fishing where you press a button to cast the rod, then have to wait for a timer and once that timer is done, it stops and a button to catch/reel in shows up.
The code works but once the timer finishes, “hi” is printed loads of times per second and im not sure why, can anyone help with making it completely stop the startTimer function?

local cast = script.Parent
local timer = script.Parent.Parent.FishingTimer
local catch = script.Parent.Parent.CatchButton

cast.MouseButton1Click:Connect(function()
	cast.Visible = false
	cast.Interactable = false
	timer.Visible = true
	
	local run = game:GetService("RunService")

	local function formatTime(currentTime)
		local hours = math.floor(currentTime / 3600)
		local minutes = math.floor((currentTime - (hours * 3600))/60)
		local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
		local format = "%02d:%02d:%02d"
		return format:format(hours, minutes, seconds)
	end

	local function startTimer(currentTime)
		run.Stepped:Connect(function(elapsed, step)
			if currentTime > 0 then
				currentTime -= step
				timer.Text = formatTime(currentTime)
			else
				print("hi")
				timer.Visible = false
				catch.Visible = true
				catch.Interactable = true
				return
			end
		end)
	end

	startTimer(5)
end)
1 Like

You never disconnected the event so it will continue to print as the current time is <= 0

how exactly should i do that? ive tried examples online but nothing works

nevermind, i got it to work lol, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.