Make module run the same function on multiple objects

I’m currently trying to make a countdown timer using 2 functions in module script.
image
Goal: My script clones a frame with text and places it inside a ScreenGui object. Then a function that converts the time given into MM:SS:MS format with another function that does the counting part, it will count down until the timer reaches 0
Issue: However, when there are multiple timers at the same time and one of them ends (Reaches 0), the rest of them also stop running. This doesn’t occur if they run one by one
When the left one reaches 0, the one on the right freezes
image
Permanently stays at 0:32:21 until it’s removed
image

Here’s the function:

local OldElapsed = 0
local Connection = nil

Module.StartTimer = function(CooldownGUI, CurrentTime)
	Connection = Running.Stepped:Connect(function(NewElapsed, Step)
		if OldElapsed ~= math.floor(NewElapsed) then
			--
		end
		OldElapsed = math.floor(NewElapsed)
		CurrentTime -= Step
		if CooldownGUI and CooldownGUI:FindFirstChild("Timer") then
			if CurrentTime <= 0 then
				CooldownGUI:Destroy()
				Connection:Disconnect() <-- Seems like this is the issue
				return
			end
			-- Function
		end
	end)
end

Other script using the function:

task.spawn(function()
	if Cooldown.Timer then
		Cooldown.Timer.Visible = true
		Modules.StartTimer(Cooldown, Time)
	end
end)

I’m guessing it’s caused by this line, it probably disconnected the other active timer if one ended while I was testing. Is there a way to run multiple threads with this function, please let me know if you can, I appreciate it!

Connection:Disconnect()

I searched on Devforum for almost 3 hours but none of the posts really helped me
Hopefully anybody can help me find a way to fix this

I didn’t even know this was a thing, but why don’t you try using a bool value instead of the disconnect?

local active

active = true

if active == true then
-- [...]
if CurrentTime <= 0 then
   CooldownGUI:Destroy()
   active = false
end

I know that in certain cases this might not be what is wanted, but I think you can connect multiple functions to a single event so :man_shrugging:. Also, if that doesn’t work, then the problem might be in the module script itself.

2 Likes

Are you sure that you aren’t destroying the Timer GUI?

2 Likes

That it what I thought at first too, but then it wouldn’t stay there after he did that.

2 Likes

My bad, had to pay more attention.

Every value that was defined before the actual function, OldElapsed and Connection in this case are shared. Whenever u run the second function, it overrides the first one. Solution would be putting local variables into the function.

Module.StartTimer = function(CooldownGUI, CurrentTime)
	local OldElapsed = 0
	local Connection = nil
	Connection = Running.Stepped:Connect(function(NewElapsed, Step)
		if OldElapsed ~= math.floor(NewElapsed) then
			--
		end
		OldElapsed = math.floor(NewElapsed)
		CurrentTime -= Step
		if CooldownGUI and CooldownGUI:FindFirstChild("Timer") then
			if CurrentTime <= 0 then
				CooldownGUI:Destroy()
				Connection:Disconnect() <-- Seems like this is the issue
				return
			end
			-- Function
		end
	end)
end
1 Like

Sorry for the late reply and thanks for the help, but unfortunately this isn’t what I was looking for

Thank you so much for the help! It’s working perfectly fine now!
About your previous question, the timer gets destroyed after a period of time but I did sanity check so it wouldn’t occur any errors
Now I don’t have to spend hours not realizing I could have done this in the first place

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