So, im doing a script for a logic gate, that the player will be able to choose from delay and hold (i dont know if i will add more later). And the code is the following:
if FT == true then -- FT is the status of the input (true or false) ACTIVE = true -- prevents plr from changing the timer mid activation IN.BrickColor = BrickColor.new("Bright blue") -- changes INPUT color --wait(TOTALTIME) -- error here!! --note: TOTALTIME is in seconds, by multiplying minutes by 60 OUT.BrickColor = BrickColor.new("Bright blue") status[INDEXOUT1] = true else ACTIVE = false -- lets the player change the timer again IN.BrickColor = BrickColor.new("Black") -- changes back color OUT.BrickColor = BrickColor.new("Black") status[INDEXOUT1] = false end
but everytime the script reads wait(TOTALTIME) it will stop everything and wait untill that is done i tried something else like:
while TIME > 0 do wait(0.001) TIME = TIME - 0.001 end if TIME <= 0 then TIMEOVER = true else TIMEOVER = false end
but its not decreasing correctly, if there is no wait() it does it instantly. but if there is a wait() it does it super slowly, not on the correct time. I have no idea of how to fix this, if someone could help me i would appreciate it alot!
All the code is being run on the same thread, which is why the loop stops everything else until it is finished. You can run the while loop on a different thread using a coroutine
Perhaps you can also try using the Heartbeat
with the Wait
function, which will wait for the event to fire, which will be every frame.
coroutine.wrap(function() while TIME > 0 do game:GetService("RunService").Heartbeat:Wait() TIME = TIME - 0.001 end end)() if TIME <= 0 then TIMEOVER = true else TIMEOVER = false end