Whats the best way to wait for long periods of time to trigger something?
I have 2 “ideas”
1 - Making a while loop (maybe RunService? Steeped? Heartbeat?) to check if the tick() value is +60 seconds in relation with the first tick() stamp. When its true, do the stuff
2 - Or just using a wait(60) ?..
Any problem with the functions in the script if its waiting for long periods of time, what about 20 minutes? What is the best way to wait for minutes?
As @dollychun said, it doesn’t matter, honestly I would pick wait() because it makes the code cleaner, anyways it depends of your script too and your needs.
Yeah well, its the same, just with a variable xD
The number still 1200, so nothing weird happens when making a script to wait for that long?
If theres no problem I will do the wait, is cheaper! Thank you guys @sfant43, @dollychun
No problem, making it multiply is better than using seconds honestly
Nothing will happen waiting for that long (Except if you are waiting with a loop, it can use resources, maybe not noticeable but is a small percentage)
Hello! I’d recommend using a wait function. While loops seem too impractical to be substituted for a wait function. There are multiple cons to using a while loop in this specific scenario, whereas a wait function is more simple. While loops can cause frame drops to clients which is self explanatory as to why.
The following statement from @sfant43 is an extremely good example as to what you can do:
It’s better to be safe than sorry, would you rather go the more simple way, or the way which can lead to more problems.
Hey there,
I’d say neither options are exactly optimal, and there are good reasons why for both. wait() will work for your case like others have said, although not as fast as you may want it to be.
I would suggest reading up on Avoiding wait() and why, as it has some valuable info that basically explains why to not use wait(). I would suggest making your own type of wait function.
If you’re going to have this repeat continously throughout the session lifetime, you should definitely use a timestep with Heartbeat/Stepped. This is simple task scheduling and is not in itself an expensive call, will not slow the client down(*) and is more reliable than wait().
An example would be if you’re restarting a round, regenerating a vehicle or similar. It will of course work with both implementations, but read the thread the poster above me referred to.
(*: unless you’re doing heavier tasks than just comparing the timestep)
Scheduling implementation example:
local timestep = 60*5 -- 5 minutes
local lastRun = timestep
game:GetService("RunService").Heartbeat:Connect(function(dt)
lastRun -= dt
if lastRun <= 0 then
lastRun = timestep -- start counting down again
-- run your code
end
end)
Yup, thank you @zQ86, I already readed that topic about wait().
Its a reason why I suggested to make a loop with RunService to read timestamps and trigger events without using wait()
But ppl told me that is causing more lag/problems, keep it simple
@Ravenshield, yup, thats is similar to the idea I had… At the end, I decided to not use the wait() method and only implementing the RunService if I was unable to find another “reason” to trigger an event. I were able to find an “action” the player is doing that looks like a good reason to trigger the event, so now Im not waiting at all.
Anyway. I will still have doubts about the 2 methods I suggested, The RunService (Heartbeat, or stepped) or the long wait()
The future will decide xD