I’ve seen a lot of people tried to make their own custom wait(). I tried using @sjr04 but it doesn’t work well for me.
THIS IS NOT SENDING HATE TO HIM.
local RunService = game:GetService("RunService")
local function Pause(n)
local accumulated = 0
RunService.Heartbeat:Wait()
while n > accumulated so
accumulated += RunService.Heartbeat:Wait()
end
end
If I use this function and tried to pause the thread for 5 seconds, it just waits for 4.5 seconds instead. I tried running multiple tests but none of them satisfies my needs. Am I doing something wrong?
EDIT 1: Turns out it works well for command bars, but if I use in scripts, doesn’t work effectively/
You should read the thread you linked. Wait on a signal returns the arguments it was fired with. Heartbeat is fired with DeltaTime, so using Wait will return DeltaTime. See RBXScriptSignal for more information on what exactly Wait does.
Still no use, doesn’t wait for the amount of seconds I want.
local Run = game:GetService("RunService")
local function Pause(n)
local accumulated = 0
local heartbeat = Run.Heartbeat
while n > accumulated do
accumulated += heartbeat:Wait()
end
end
local start = os.time()
Pause(5)
print(os.time() - start)
Worth noting that I also switched to os.clock so I could get a more precise measurement of how much time was being elapsed before and after the pause call, since os.time only returns whole seconds rounded. In all cases I’m reaching close to 5 seconds.
Ultimately not sure where you’re running this from or the specs of your device though considering Heartbeat is variable according to the current machine’s specs.
Could you give more information on your testing environment or your device’s specs? You could additionally check more variables in your script such as the DeltaTime being observed by the current iteration or your total time accumulation.
It is possible to create a custom wait and this is pretty much the way it’s done but it is very critical to note that point about RunService events being variable on the current machine. It’s certainly waiting 5 seconds for me but if not for you, some debugging is in order.
I’ve gone ahead and added some markers on the code you replied to me with:
local Run = game:GetService("RunService")
-- {accumulatedTime: deltaTime}
-- Check what the deltaTime was for an accumulated time point
local increments = {}
local deltaTimes = {}
local function Pause(n)
local accumulated = 0
local heartbeat = Run.Heartbeat
while n > accumulated do
local dt = heartbeat:Wait()
accumulated += dt
increments[accumulated] = dt
table.insert(deltaTimes, dt)
end
end
local start = os.clock()
Pause(5)
print("Time to run:", os.clock() - start)
print("Lowest delta:", math.min(table.unpack(deltaTimes)))
print("Highest delta:", math.max(table.unpack(deltaTimes)))
print("Exact increments", increments)
local RunService = game:GetService("RunService")
local function wait(n)
n = typeof(n) == 'number' and n or 0
local spent = 0
repeat
spent += RunService.Heartbeat:Wait()
until spent >= n
end
Well, I had this other implementation:
(it just makes sure using os.clock, so it actually takes the certain amount of seconds, SINCE the wait was called, not since the last frame was called, if you want to use delta time from wait, or you’re messing with things that are important to respect to frames, use the function above)
local RunService = game:GetService("RunService")
local function wait(n)
n = typeof(n) == 'number' and n or 0
local start = os.clock()
local spent = 0
while true do
repeat
spent += RunService.Heartbeat:Wait()
until spent >= n
spent = os.clock() - start
if spent >= n then
return spent, os.clock()
end
end
end
Your issues are kind of weird? I’ve never seen anything like that happen to anyone.
Debugging purposes: one is a dictionary, one is an array. The dictionary is meant to log what the deltaTime is at a certain point of accumulation (when Heartbeat fires) and the array is meant to collect all deltaTimes that have occurred in an array format for checking the minimum and maximum values that get assigned to deltaTime. This can help debug time issues.
local AccumulatedTime = 0
game:GetService("RunService").Heartbeat:Connect(function(delta)
if AccumulatedTime > 5 then
print("Waited five seconds")
else
AccumulatedTime += delta
end)
Explanation: .Heartbeat fires every frame, and a new frame will optimally be created every 1/60 of a second. If for some reason, perhaps due to lag, your frame rate drops, a new frame may be created every 1/50 of a second, or 1/42, or even 1/23. You get the idea. Deltatime is the time between frames. If your framerate is low, deltatime will be higher. By adding deltatime, you can correct time lag.
However, if you have a very low framerate then by the time .Heartbeat fires again, time will have passed and you will have overshot.