Os.clock not working

im trying to make a timer gui that starts when a player touches a specific part

local part = script.Parent

	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	local humanoidrootpart = hit.Parent:FindFirstChild("HumanoidRootPart")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if humanoid then
		humanoidrootpart.CFrame = CFrame.new(-281.113, 87.246, -388.14)
local localplayer = game.Players.LocalPlayer
local gui = localplayer.PlayerGui:FindFirstChild("Timer"):FindFirstChild("TextLabel")
local run = game:GetService("RunService")

	local starttime = os.clock()
	local Timer = run.Heartbeat:Connect(function()
		local Elapsed = os.clock() 
		local Minutes = math.floor(Elapsed / 60)
		local Seconds = math.floor(Elapsed % 60)
		local Milliseconds = (Elapsed % 1) * 100
		gui.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)

but whenever the timer starts the timer doesnt start from 00:00.00

You didn’t get the elapsed time:

local Elapsed = os.clock() - starttime

os.clock is different from os.time.

os.time() is the UTC epoch seconds counter.

For your case I’d use tick() as it’s a non-whole number and is more precise, though.

tick() returns an integer. OP wants milliseconds, so that doesn’t really work. os.clock is fine to use here

tick() returns a float. os.time() returns a counter as an integer. os.clock() also returns a float, but should be used for different purposes.

1 Like

Oh, my bad.

I looked into it a bit more and this table sums it up:


tl;dr os.clock is fine, and tick is getting deprecated sometime™ in the future (see main post in Source)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.