Help with Heartbeat and os.time()?

I’m currently trying to recreate

while true do
	wait(1)
end

using Heartbeat.

I want to know two things: Is this the best way to do this, and how can I make it not in event form, meaning is there a way to make it not create a new thread every time?

local Interval = 1
local NextCheck = 0

RunService.Heartbeat:Connect(function()
	local CurrentTime = os.time()

	if CurrentTime < NextCheck then
		return
	end

	NextCheck = CurrentTime + Interval
end)

You can use RunService.Heartbeat:Wait() in a loop. I’d also recommend using tick() instead of os.time(), but the difference might be negligible. I think tick() is more commonly used for this kind of thing - not entirely sure why, but there’s probably a reason.

However I will say that the difference between the event and using :Wait() will be pretty minimal too.

local Interval = 1
local NextCheck = 0

while true do
	RunService.Heartbeat:Wait()
	
	local CurrentTime = tick()
	
	if CurrentTime < NextCheck then
		return
	end
	
	NextCheck = CurrentTime + Interval
end

So like this?

1 Like

Accidentally posted it, had no time to edit. My bad, I’ll just rewrite the thing.