Is while loop similar to run service heartbeat

i was just curious, because on a while loop you have to wait or else it’ll break or even crash the game

im using task.wait cause its more preferred rather than just wait and it also runs on heartbeat and the amount of wait also depends on something called variable frequency i think its relies on the framerate of the client/server

here’s the documentation for both task wait and heartbeat
https://developer.roblox.com/en-us/api-reference/lua-docs/task

https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat
if you want you can test out these two pieces of code in 2 separate scripts

while task.wait() do
	print("hi")
end
local RunService = game:GetService("RunService")

local function epic()
	print("hi")
end

RunService.Heartbeat:Connect(epic)

well here’s my logical reasoning why they are both similar, first they both repeat the same thing over and over, and second they both depend on heartbeat until they can do anything, and lastly i can’t tell the difference between these two in performance wise

and alright sorry for the ramble, but which one is better in general? or are they both the same in functionality and performance wise?

They way you’re using them, I don’t see anything that’d make them any different.

task.wait has the additional functionality that you can pass a number as a parameter, making it wait the given amount of time.

Heartbeat.Connect has the added functionality that it returns a Connection object, allowing you to disconnect it again to make it stop repeating.

No, theres a difference.

Heartbeat event fires every frame, if your frame drops then the amount of firing heartbeat events drop too. (thats how most of the game have FPS indicator because they use heartbeat, One of the game with FPS indicator feature: Arsenal)
While while loops works continuously no matter what your frame rate is.

task.wait still has to wait to variable frequency until it can do something, doesn’t matter how many fps it still has to wait until it can do something, because it is being yielded by the wait

local run = game:GetService("RunService") print(run.Heartbeat:Wait(), task.wait())

they both work in similar manner

@IceCreamPickels This is actually inaccurate. task.wait() resumes on the next invocation point, so if it was called during the invocation point of render stepped, it would resume on the invocation point of stepped and etc, whereas Heartbeat:Wait() resumes on the next heartbeat invocation point.

local RunService = game:GetService("RunService")

print(task.wait()) --> approx 1/60
print(RunService.Heartbeat:Wait()) --> approx 1/60
RunService.Stepped:Wait()
print(task.wait()) --> less than 1/60
print(RunService.Heartbeat:Wait()) --> approx 1/60

@Synitx It is better not to be seemingly overconfident about something which is false. While loops “being laggy” make absolutely no sense, it all depends on the rate you are resuming other code at yielding and what type of work you’re performing (read more about this via the info on task scheduler).

while true do
     wait() --> resumption rate approximately 1/30, i.e approx 2 frames
end

-- vs

while true do
     game:GetService("RunService").Heartbeat:Wait() --> resumption rate approximately
    -- 1/60, i.e approx 1 frame
end
1 Like