I just want to know if theres a way to implement deltaTime into a task.wait().
Mostly because people might have different FPS, and waiting a second would be more like waiting half a second or 2 seconds.
I am aware delta time is the time between two frames, which is why im wondering if theres a way to have a more accurate second(s) in a task.wait(). This would be very helpful if gameplay relies on a timer (possibly involving miliseconds accuracy), especially on a client
Quick example for a scenario:
local bombTime = 300
for i = bombTime, 0, -1 do
task.wait(0.1)
--insert awesome timer display with string.sub or stuff too lazy to write that lol
if i == 0 then
print("exploded! Failed to defuse")
end
end
if the player is running on a higher fps, the bomb will detonate quicker, and same for lower making it slower
FYI: When I searched for solutions beforehand, it was either complicated without using task.wait() or ‘use this formula and method because it works trust me’ without giving a clear straight foward answer. So i also want to know howdeltaTime can be used and how it works, thanks!
local bombTime = 300
local elapsed = 0
while elapsed < bombTime do
local dt = task.wait()
elapsed += dt
local remaining = math.max(0, bombTime - elapsed)
print("Time left: " .. math.floor(remaining))
if remaining <= 0 then
print("exploded! Failed to defuse")
break
end
end
When you call task.wait(n), you’re basically suspending the current thread and scheduling it to be resumed in at leastn seconds. Underneath, the task scheduler is pretty much running in its own loop and checking how much time has passed. If elapsed time >= n, it resumes the thread.
The consequence of this is that the time it takes to actually resume the thread is dependent on how often the scheduler is cycling (usually 60 Hz, so 60x each second). Because of this, that wait could actually take n + 1/60 seconds.
For your example, the drift compounds because you’re repeatedly waiting every iteration, so you could be waiting (0.1 + 1/60) * 300 = 305 seconds.
What the method given above does is manually count the time passed so you can get how much time is remaining while also keeping an error of only +1/60.
Thanks for the explanation, however I do have a question. Is 1/60 the deltaTime here? If so, can i just do (n + deltaTime) * goal? or do I have the equation wrong.
I also wanna point out in the example being provided, the task.wait(0.1) im using is basically telling bombTime is in miliseconds, yknow? For simplier terms we can just say bombTime = 30 and task.wait(1) (saying this just in case if theres confusion, dont worry if theres not)
Im not sure about task.wait, why dont you use RunService? Spscifically heartbeat or renderstepped, depending on your need and script. It gives you delta time, making your life much easier (i might misunderstood this post, pleass dont yell at me)