Hello! The following code I put down is an example of my problem. So I want a repeat function which would grant me a 30 second timer. In the repeat function, I add a “task.wait(1)” so the timer goes down 1 every second, like any normal timer. Problem is, I want the wait function to break immediately if a parameter has changed. How could I do that?
local value = 1
local timer = 30
repeat
timer = timer - 1
task.wait(1) -- even during the wait, i want to check during every frame of the game at all if the value variable ever becomes equal to 0
if value == 0 then
timer = 0
end
until timer == 0
print("this would print immediately if value equals 0")
Yes, but the problem is I need that print to print immediately if value equals 0. If the task.wait(1) starts and all the sudden value equals 0, it would wait a whole second before checking if value equals 0, creating a delay which I don’t need.
local Value = 1
local Timer = 30
task.spawn(function()
while true do
Timer -= 1
if (Value == 0) then
break
else
task.wait(1)
end
end
print(Timer, "seconds left on the timer.")
end)
Yes, so basically in the repeat function, I’ve created a timer. Every time it waits 1 second, the timer would minus 1. It would keep doing that until it hits 0. Now, I need to check if the value variable (which I’ve set to 1) ever gets set to 0. I need it to check if it hits 0 every frame that passes by. If it hits 0, I need the wait function to break immediately. Then the if statement would check if it’s 0 and because it is, it would set timer to 0. And because the repeat function keeps looping until timer equals 0, then it would stop and move on to the next line, which would make the print happen. I just don’t want a one second delay if value equals 0 right as the wait function starts.
local parameter = 5 -- the parameter that can be changed
function repeatFunction()
local timer = 30 -- set the timer to 30 seconds
while timer > 0 do
if parameter == 0 then
print("Timer stopped because parameter is 0")
break -- stop the loop if the parameter is 0
end
task.wait(1) -- wait for 1 second
timer = timer - 1 -- decrement the timer by 1 second
end
print("Timer ended")
end
spawn(function()
wait(5) -- wait for 5 seconds before changing the parameter
parameter = 0 -- change the parameter to 0
end)
repeatFunction() -- start the repeat function
Alright, but for example, if the task.wait(1) just started and then parameter equals 0 at the same time, would it wait the second before checking if parameter equals 0? I would not only need it to check at all times if it would equal 0, but also to break the task.wait(1) so it moves on right away.
local parameter = 5 -- the parameter that can be changed
function repeatFunction()
local timer = 30 -- set the timer to 30 seconds
while timer > 0 do
if parameter == 0 then
print("Timer stopped because parameter is 0")
break -- stop the loop if the parameter is 0
end
local waited = false
local thread = coroutine.create(function()
task.wait(1)
waited = true
end)
repeat
coroutine.resume(thread)
until waited or parameter == 0
if parameter == 0 then
print("Timer stopped because parameter is 0")
break -- stop the loop if the parameter is 0
end
timer = timer - 1 -- decrement the timer by 1 second
task.wait(1) -- wait for 1 second before checking the parameter again
end
print("Timer ended")
end
spawn(function()
wait(5)
parameter = 0 -- change the parameter to 0
end)
repeatFunction() -- start the repeat function
Hello! Thanks for replying. In the middle of that task.wait(1), I changed the parameter to 0, so the task.wait() had to finish before checking if it turned to 0. I would need it to check if it turns 0 and if it did, then to stop the task.wait() and move on to the next piece of code of what would happen when the timer finishes. I found a solution by adding another timer called timer2, and timer2 is set to 10, and I use a repeat function to repeat task.wait(0.1) until the timer hits 0, making it wait a second. I also check if the parameter has changed every 0.1 seconds. If it has, I break it immediately so it goes to the next line of code. Thank you for responding though, I much appreciate it!