Hey!
I’m looking for a way to create a loop that will run once a second. The issue is that I need to make sure there is no time distortion based on anything like lag.
I also need it to run in a server script.
Is using a while wait(1) do
time accurate no matter what? If it isn’t, is there some other way to continuously loop in a precise time interval?
Thank you!
Maybe something like this might have a little less delay than the built-in wait() function but I wouldn’t really worry about it unless you can notice that there is a delay.
local RunService = game:GetService("RunService")
wait(5) --wait until the game is loaded
local function wait(t)
local w = 0
repeat
local p = RunService.Heartbeat:Wait()
w += p
until w > t
end
local t = os.clock()
wait(1)
print(os.clock() - t) --> 1.001 / 0.998...
built-in wait()
wait(5) --wait until the game is loaded
local t = os.clock()
wait(1)
print(os.clock() - t) --> 1.016...
Hey, @GEILER123456
I said I wanted this to run on the server, I can’t use RunService
on the server though.
You can use the RunService.Heartbeat event on the server. It basically fires every frame after the physics simulations.
1 Like