A custom wait function can be made in 2 ways:
- You turn your thread in a ROBLOX thread (one that's in the internal taskscheduler)
- You make your own task scheduler that runs in another thread
By default, every thread, except ones created with the coroutine library are ROBLOX threads.
Since you want a custom wait, I assume you want to use it in coroutines.
You'll have to use some kind of yielding (except co.yield) in your thread scheduler though.
The easiest way would be wait(), but on the client you could use RenderStepped:wait() etc.
I'm gonna use wait() here since that's easier to write
You'll first need a task scheduler for that:
02 | local function startthread(func,...) |
03 | local th = coroutine.create(func) |
04 | table.insert(threads,th) |
05 | return coroutine.resume(th,...) |
09 | for k,v in pairs (threads) do |
10 | if coroutine.status(v) = = "dead" then |
15 | local s,e = coroutine.resume(v) |
18 | warn( "Thread error:" ,e) |
Your custom wait would be in all cases something like this:
5 | repeat coroutine.yield() until tick() > stop |
7 | return tick()-start,tick() |
How this works:
Start a thread by calling startthread(FUNCTION,ARG1,ARG2,...)
Your wait function will yield the current coroutine. (the repeat loop)
Your task scheduler will resume (all) the thread(s) every wait().
Your wait function gets resumed and checks if it waited long enough.
If it didn't, it'll just yield again and again, until enough time has passed.
Note: If you use wait() inside the new thread, your thread will become a ROBLOX thread.
(Which means, the thread is added to ROBLOX' internal thread scheduler)
This would make roblox auto-resume the thread the whole time.
Just stick with wait(), it's the easiest, it works, and you don't need a custom 99.99% of the time