Ad
Log in to vote
0

Use something else rather than Wait()?

Asked by 21 days 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 — 21d
0
You really need to use wait() (or task.wait() rather) in a loop otherwise it will break your game. T3_MasterGamer 1701 — 20d
0
Also can you please explain more of what you're trying to do? It's confusing for me.. T3_MasterGamer 1701 — 20d

1 answer

Log in to vote
0
Answered by 21 days ago
Edited 20 days 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 1701 — 20d
0
Oh. You are correct, I was not aware that they were the same, thanks. xInfinityBear 1691 — 20d
Ad

Answer this question