Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Use something else rather than Wait()?

Asked by 1 year ago

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!

0
im trying to use delay() to see if it works, still testing it tho JP_Pontes 5 — 1y
0
You really need to use wait() (or task.wait() rather) in a loop otherwise it will break your game. T3_MasterGamer 2189 — 1y
0
Also can you please explain more of what you're trying to do? It's confusing for me.. T3_MasterGamer 2189 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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
0
Isn't task.wait() equivalent to Heartbeat:Wait()? Perhaps you could try that instead. T3_MasterGamer 2189 — 1y
0
Oh. You are correct, I was not aware that they were the same, thanks. xInfinityBear 1777 — 1y
Ad

Answer this question