How to make wait() only affect a part of script, not whole script?

Is there an alternative to wait() which only affects part of a script, like a function, not the whole script? I kinda need this wait() but then again it ruins the whole script.
I want it to wait on a certain thing, but have other things run next to it while its waiting, not have to wait until the wait is over for the other things to run

4 Likes

You’ll be looking for delay(). Wrap that around what ever you want to delay, like this:

delay(1, function() -- 1 is delay time
    -- blah blah
end)
-- other stuff will still run

However, delay is sometimes very unrealiable, so a better version is using coroutines. Like delay, which create a new thread letting the current one still run.

Like so:

local func = coroutine.create(function()
    wait(1)
    -- blah blah
end)
coroutine.resume(func) 

This is just one way to do it have a look at the wiki post for more

6 Likes

Would this delay the entire function?

Anything you put inside the delay will run after the set time you put and everything after the delay will run like the delay doesnt exist for example

delay(1, function() -- 1 second delay
    print("1")
end)
print("2")

2 would print first then one second later 1 will print

6 Likes