Tick() or wait() ? Wait for minutes?

Hiwi. Another stupid question by me :3

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?

Thank you for reading!

2 Likes

To be honest, I think it doesn’t matter,

While Loop:
The game lags a bit, but the waiting time is precise

Wait:
The wait time is a bit less precise (0.05s?), But it doesn’t use much resources.

2 Likes

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.

2 Likes

So I do this? wait(1200) to wait 20 minutes?.. looks weird…

1 Like
local minutes = 5
wait(60 * minutes)
6 Likes

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

1 Like

No problem, making it multiply is better than using seconds honestly :stuck_out_tongue:
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)

2 Likes

Perfect then, I’ll go for wait()!! And I agree, looks cleaner using a variable with minutes :3

2 Likes

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.

Hope my suggestion helped! :roblox_light:

3 Likes

I totally agree! Now I feel more confident with the waits, thank you :3

2 Likes

Glad I could be of service! Good luck.

2 Likes

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.

2 Likes

I disagree with the previous posts.

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)
4 Likes

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

2 Likes