I want to know:
Which is more efficient for performance and which is better for waiting, even if the server is lagging?
wait(1)
or
repeat
wait()
until (tick() - oldTick() >= 1
I want to know:
Which is more efficient for performance and which is better for waiting, even if the server is lagging?
wait(1)
or
repeat
wait()
until (tick() - oldTick() >= 1
just use
wait(1)
than spammy
repeat
wait() -- you used the same function
until (tick() - oldTick()) >=1
I guess tick() is deprecated.
And wait() is not accurate enough so I would recommend you Custom Wait function if you want it to be precise or things such as RunService waits.
game:GetService("RunService").Heartbeat:Wait()
Yes, but on laggier servers, wait(1) can wait up to 10 seconds, instead of waiting 1 second. Tick() can be good regardless of lag, I just want to know which one is better in most situations.
Both of these methods are inefficient because you’re unnecessarily polling, but the first one is a little better than the second.
tick
updates every second, then why are you waiting roughly 1/30th of a second every time to see if a second has passed?
If you really care about such precision, then use os.clock
as a replacement for tick
when it gets deprecated. Performance wise, the difference is negligible, don’t go ahead looking for which one is more faster, don’t be that one guy who micro optimizes everything in his code.
The best you can do is use a custom wait function for the outmost accuracy (only if you really care about precision, otherwise use the global wait
) and use os.clock
as it has the highest accuracy, which makes it a much better replacement over tick
which is soon going to be deprecated anyways.
local RunService = game:GetService("RunService")
local function HeartBeatWait(yield)
if typeof(yield) ~= "number" or yield == 0 then
return RunService.Heartbeat:Wait()
end
local dtPassed = 0
while true do
if dtPassed >= yield then
return dtPassed
end
dtPassed += RunService.Heartbeat:Wait()
end
end
local before = os.clock()
--[[
you can also just use HeartBeatWait(1),
but this is just for the sake of ur question
]]
while (os.clock() - before) < 1 do
HeartBeatWait(1)
end