However, I have no idea how to do it so that when timeValue is changed, it checks if there is an existing “timer()” function running, it completely stops “timer()” function and starts a new “timer()” function on the new timeValue.
local Value = game:GetService("ReplicatedStorage").Value
local function printtime(thetime)
print("Value changed to ".. thetime)
end
Value.Changed:Connect(function(change)
printtime(change)
end)
Remember: If you are writing this in server script then you have to change the value via Server, not the client!
The client can detect what server changes but server cannot detect what client changes.
Um what’s the difference of this from what I wrote, and I’m trying to stop the old running function everytime Value is changed and run a new “printtime()” function. I don’t think this will work.
I have no idea what is inside the timer() function but judging by its name I think I can guess it has some kind of while loop or a connection to RunService.Heartbeat for a timer?
Now if I was right that there is a while loop or a Heartbeat connection inside timer() you can just have a variable at the top that you set to false when starting a new timer
A simple example I can get off the top of my head is:
local runTimer = false
function timer()
if runTimer then -- check if one is running and stop it
runTimer = false
-- you'll want to wait for the timer to end here maybe with a event or just task.wait I'll use task.wait for simplicity
task.wait(.3)
end
while runTimer do
-- timer stuff here
task.wait(.3)
end
runTimer = true -- set it to true for when we start the next one
end
function timer(seconds)
for i = 1, seconds, 1 do
--blah
task.wait(1)
end
end
And what I mean is that I want to stop an existing function and start a new one. ( For example while the timer() is running, I pause the function and start another timer() function since the “seconds” parameter will be different everytime I run the timer() function.
simply just check for the timer value before calling the func:
timeValue.Changed:Connect(function(change)
if change <= 0 then
-- the timer will start if it hasn't started yet
timer(change)
else
-- Already counting
end
end)
Now it’s just stuck at the else statement, doing nothing, not even counting the time.
Actually is it possible to use things like coroutine or something? I heard of them but I don’t really get how to use them in this scenario…
My solution should actually work here just in a different way.
you could still have a variable at the top that says if a timer is running and check whenever you start the function
Using this it would look something like this
local TimerRunning = false
function timer(seconds)
if TimerRunning then
TimerRunning = false
task.wait(1) --// you could use a bindable event that fires every time it loops but this will work
end
TimerRunning = true
for i = 1, seconds, 1 do
if TimerRunning == false then
break --// break out of the timer if we tell it to stop
end
--blah
task.wait(1)
end
end
As for coroutines you should be using one so that the timer doesn’t stop code from running(if there was code after you called timer() it would have to wait for the timer to end before continuing) but they won’t help you in stopping the function.