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

How do I do a wait(1) in a while true do loop without delaying the loop by 1 second?

Asked by 3 years ago
Edited 3 years ago

I'm trying to wait 1 second then destroy an object, but it delays the loop by one second. I've tried to use the variable outside of the loop, but that doesn't work either.

note: i'm cloning a bullet shell locally

1 answer

Log in to vote
1
Answered by 3 years ago

You can do this multiple ways, here is a couple...

while true do
    local part = Instance.new("Part)
    part.Parent = workspace

    game.Debris:AddItem(part, 1) -- Gets destroyed after 1 second
end

or

while true do
    local part = Instance.new("Part)
    part.Parent = workspace

    coroutine.wrap(function()
        wait(1)
        part:Destroy()
    end)()
end

I prefer the first example, but sometimes the second one is necessary for specific reasons.

0
you god Vexanonymous 6 — 3y
Ad

Answer this question