I don’t believe anybody asked for this, but I always find it annoying that I have to reconfigure a whole script just so this one wait doesn’t pause the thread under- but I have a problem with a script I’m using, so I came up with a solution.
The script would pause the while
loop at the bottom of my script, and I can’t put the code under the loop or the other code won’t run! So, I came up with a larger, but better solution.
And yes, I do know I could move the while
statement to another script, but I like keeping everything in 1 LocalScript and 1 ServerScript (if needed).
How to use it
Instead of doing this (example):
wait(1)
-- some code that uses a wait
while wait() do
-- some code that should always be running even when in a wait and is too fast when using a Heartbeat statement
end
Do this (example):
local t = tick()
timer = game:GetService'RunService'.Heartbeat:Connect(function()
if (tick()-t) < TIMETOWAIT then return end
-- some code that uses a wait
timer:Disconnect() -- stop the code from rerunning
end)
while wait() do
-- some code that needs to be always running and now can because it doesnt have a wait() at the top :D
end
How it works!
Pretty much it does this:- local t = tick()
Gets the time from January 1st, 1970 to now and puts it into a variable called t
- timer = game:GetService'RunService'.Heartbeat:Connect(function()
Creates a thread of RunService’s Heartbeat (Can also use RenderStepped I think) and puts it into a global named timer
- if (tick()-t) < TIMETOWAIT then return end
This then gets the new time from January 1st, 1970 to now and subtracts it from the original to get the amount of seconds since the t
variable was created and compares it with a <
to the amount of seconds you’d like it to wait() for and if its not past, it’ll return
and not run the rest of the code.
- -- some code that uses a wait
Just replace this with your code lol
- timer:Disconnect
This stops the thread from running again so it doesnt make the code run over and over again, which would be very bad.
- end)
Finishes off the function !
Conclusion & Poll
Please note: This is my first tutorial on here, please don’t judge that its basic.
If you think you might use this, please vote that in the poll below! If not, vote otherwise :]
- I won’t use this
- I might use this in the future
- I’ll use it at some point.
0 voters
Thanks for reading!