Every frame, you’re asking it to wait 5 seconds then print test. So every frame, another wait starts. After 5 seconds, it prints test. Then the next frame, another wait finishes and prints test.
It ignores the wait because it is an event, it runs everything the condition for the event to happen it met, in the case of heartbeat, physics have been calculated and so it runs the event, and once another event happens, it’ll run that code separately, unlike a while loop which respects waits,
You need to debounce the event to make it work like a while loop, respecting waits and what not
No, it’s not ignoring the wait(5) at all. A place with this script will have nothing print out for the first 5 seconds, then it will suddenly start printing every frame. Stepped fires about 60 times per second, and each time, it’s waiting 5 seconds and then printing “test” when the thread resumes. If you’re not convinced it’s not being ignored, change the wait(5) to wait(60) and notice that you’ll have no output for the first whole minute, then “test” printing at 60fps after 1 minute.
The Heartbeat version of a once-every-5-seconds tick looks like this:
local elapsed = 0
game:GetService("RunService").Heartbeat:Connect(function(dt)
elapsed += dt
while elapsed >= 5 do
elapsed -= 5
print("test")
end
end)
When the delay is this long, you don’t need it to be a while loop, it can just be an if elapsed >= 5 then, but I gave you the general format that works for any delay duration, even sub-frame timings.