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

How would I skip over a wait() before it has ended?

Asked by 5 years ago

Instead of using a for loop or something similar, is there an easier (maybe built-in) way to go past the wait?

-- code
wait(1000) -- i want to skip this wait
-- code
0
Please, explain a little more StarLordEnzoGamer 17 — 5y
0
Wait() yields the thread, use delay instead of wait(). Theswagger66 54 — 5y

1 answer

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

use coroutines or spawn. they make a new thread (spawn has a 1/30th of a second delay but is less complicated than coroutines.) this allows you to have multiple blocks of code running at the same time.

for example:

spawn(function(a) --//spawn requires a function to execute. not a function call, but the function variable itself.
    wait(5)
    print('I called this function in another thread after 5 seconds.')
end)
wait(1)
print('I called this function in the main thread after 1 second.')

and then there's coroutine. nobody likes coroutine. there are multiple ways to run coroutine.

number 1: there is coroutine.create and coroutine.resume. resume takes the created coroutine and executes--it is a variadic function, meaning it can take an endless amount of variables. take note that it's like calling a method but using a dot, kind of. it requires the coroutine that you wish to call as a first argument. the variadic comes after that.


local Coroutine = coroutine.create(function(x,y,z) print(x^2+y^2 == z^2) end); coroutine.resume(Coroutine, 15, 15, 450) --// returns true or false depending if your arguments follow the pythagorean theorem

number 2: there is coroutine.wrap. it's like calling a function. but with a coroutine. it's weird, i know.


local Coroutine = coroutine.wrap(function(x,y,z) print(x^2+y^2 == z^2) end); Coroutine(15, 15, 450) --//same as above, really.

beginner's guide to coroutine and spawn.

0
No one hates coroutines, where did you get that from? Theswagger66 54 — 5y
0
joke: a thing that someone says to cause amusement or laughter, especially a story with a funny punchline. Fifkee 2017 — 5y
0
furthermore, i do. screw coroutines. Fifkee 2017 — 5y
Ad

Answer this question