It is not necessarily true. I ran some tests with your module and the regular “loop with Heartbeat:Wait()”. The result showed “loop with Heartbeat:Wait()” is slightly more accurate. My test code and outputs are provided below.
Test code:
local o_clock = os.clock
local c_yield = coroutine.yield
local c_running = coroutine.running
local c_resume = coroutine.resume
local Yields = {}
game:GetService("RunService").Stepped:Connect(function()
local Clock = o_clock()
for Idx, data in next, Yields do
local Spent = Clock - data[1]
if Spent >= data[2] then
Yields[Idx] = nil
c_resume(data[3], Spent, Clock)
end
end
end)
local function wait2(Time)
Time = (type(Time) ~= "number" or Time < 0) and 0 or Time
table.insert(Yields, { o_clock(), Time, c_running() })
return c_yield()
end
local function loopHeartbeat(seconds)
local elapsed = 0
local timeout = seconds or 0
repeat
elapsed = elapsed + RunService.Heartbeat:Wait()
until elapsed > timeout
return elapsed
end
wait(10) -- Wait 10 seconds for "Play Solo" to be stable
local function test(waitFunction, runCounts)
local totalTime = 0
runCounts = runCounts or 30
for i = 1, runCounts do
local waited2 = waitFunction(1)
totalTime += waited2
print(waited2)
end
local averageWaitTime = totalTime / runCounts
print("Average wait time: ", averageWaitTime)
return averageWaitTime
end
print("Pyseph's BetterWait")
local result1 = test(wait2)
print("Loop with Heartbeat:Wait()")
local result2 = test(loopHeartbeat)
local delta = result2 - result1
print("Final Results: ")
print("Pyseph's BetterWait average wait time " .. result1)
print("Loop with Heartbeat:Wait() average wait time " .. result2)
print(
"More Accurate Function: "
.. (delta < 0 and "Loop with Heartbeat:Wait()" or "Pyseph's BetterWait")
.. " by "
.. delta
)
Output:
17:36:16.164 Pyseph's BetterWait - Playground:143
17:36:17.180 1.0151325040024 - Playground:134
17:36:18.196 1.0163139370015 - Playground:134
17:36:19.197 1.0007079129973 - Playground:134
17:36:20.214 1.0163980680009 - Playground:134
17:36:21.229 1.0155252889999 - Playground:134
17:36:22.230 1.0004381969993 - Playground:134
17:36:23.246 1.0161530400001 - Playground:134
17:36:24.264 1.0170518500017 - Playground:134
17:36:25.280 1.0155368090018 - Playground:134
17:36:26.281 1.000763101998 - Playground:134
17:36:27.297 1.0158456979989 - Playground:134
17:36:28.297 1.0000290450007 - Playground:134
17:36:29.314 1.0163870389988 - Playground:134
17:36:30.330 1.0162918210008 - Playground:134
17:36:31.347 1.0161106450032 - Playground:134
17:36:32.364 1.016458170001 - Playground:134
17:36:33.380 1.016455691999 - Playground:134
17:36:34.397 1.0159157490016 - Playground:134
17:36:35.413 1.0164659180009 - Playground:134
17:36:36.430 1.0164859329998 - Playground:134
17:36:37.446 1.0157243880021 - Playground:134
17:36:38.463 1.0169294400002 - Playground:134
17:36:39.480 1.0161411480003 - Playground:134
17:36:40.480 1.0000558619977 - Playground:134
17:36:41.497 1.0163700119992 - Playground:134
17:36:42.514 1.016410242999 - Playground:134
17:36:43.530 1.0162811160008 - Playground:134
17:36:44.547 1.0162930980005 - Playground:134
17:36:45.563 1.016094224 - Playground:134
17:36:46.580 1.0163273530015 - Playground:134
17:36:46.580 Average wait time: 1.013569776767 - Playground:138
17:36:46.580 Loop with Heartbeat:Wait() - Playground:146
17:36:47.564 1.0000607641414 - Playground:134
17:36:48.564 1.0002627000213 - Playground:134
17:36:49.564 1.0000321837142 - Playground:134
17:36:50.564 1.0001076254994 - Playground:134
17:36:51.581 1.0168677251786 - Playground:134
17:36:52.597 1.016533896327 - Playground:134
17:36:53.613 1.0155636835843 - Playground:134
17:36:54.614 1.0010616341606 - Playground:134
17:36:55.631 1.0166026903316 - Playground:134
17:36:56.646 1.0149364760146 - Playground:134
17:36:57.647 1.0009081605822 - Playground:134
17:36:58.663 1.0167570821941 - Playground:134
17:36:59.664 1.0002399636433 - Playground:134
17:37:00.664 1.0002204701304 - Playground:134
17:37:01.680 1.0162733001634 - Playground:134
17:37:02.696 1.0162025485188 - Playground:134
17:37:03.713 1.0166019331664 - Playground:134
17:37:04.730 1.0164914987981 - Playground:134
17:37:05.732 1.0022880947217 - Playground:134
17:37:06.746 1.014188173227 - Playground:134
17:37:07.746 1.0003567114472 - Playground:134
17:37:08.747 1.0000508250669 - Playground:134
17:37:09.763 1.0163883510977 - Playground:134
17:37:10.763 1.0000830888748 - Playground:134
17:37:11.763 1.000117902644 - Playground:134
17:37:12.763 1.0001743398607 - Playground:134
17:37:13.779 1.0156670575961 - Playground:134
17:37:14.780 1.0006323931739 - Playground:134
17:37:15.797 1.0167331211269 - Playground:134
17:37:16.813 1.0167141389102 - Playground:134
17:37:16.813 Average wait time: 1.0083039511306 - Playground:138
17:37:16.814 Final Results: - Playground:151
17:37:16.814 Pyseph's BetterWait average wait time 1.013569776767 - Playground:152
17:37:16.814 Loop with Heartbeat:Wait() average wait time 1.0083039511306 - Playground:153
17:37:16.814 More Accurate Function: Loop with Heartbeat:Wait() by -0.0052658256364035 - Playground:154
Edit: Swap “Heartbeat” with “Stepped” is more accurate.
local function loopStepped(seconds)
local elapsed = 0
local timeout = seconds or 0
repeat
local _, delta = RunService.Stepped:Wait()
elapsed = elapsed + delta
until elapsed > timeout
return elapsed
end