How should i keep track of time heartbeat step or time()

this probably doesn’t make a difference in the matter but im just wondering what would better to use for accuracy and performance

its basically a second counter clock

let me show you what i mean

this with heartbeat step

local RunService = game:GetService("RunService")
local total_time = 0

local loop = RunService.Heartbeat:Connect(function(step)
	total_time += step
	if math.floor(total_time) < total_time - step then return end
	print(math.floor(total_time))
end)

with time()

local RunService = game:GetService("RunService")

local debounce = 0
local total_time = 0

local loop = RunService.Heartbeat:Connect(function()
	if time() < debounce + 1 then return end --if current time is greater than debounce + 1 second
	debounce = time()
	total_time += 1
	print(total_time)
end)

here’s heartbeat
https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

i got other reasons why i don’t want to use a for loop with a wait, i wanna minimize yielding as much as possible and also be able to stop it as fast as i can

Any. The performance difference is incredibly negligible.

However, to be the most accurate, you’ll probably want to use Roblox’s provided timekeeping functions.

local startTime = os.clock()

function heartbeat()
    local currentTime = math.floor(os.clock() - startTime)
    -- do stuff here
end

If you’ve been watching me, you’ve noticed I’ve changed time functions about three times now.

See this table.

1 Like

here is a video i made talking about this exact topic

2 Likes