Help with Timer counting minutes, seconds and milliseconds

So as i’m working on my JToH Fangame, i really wanted a timer. However, i dont know what scripts to use for a timer thats counting minutes, seconds and milliseconds.

Any help would be appreciated! :slightly_smiling_face: :+1:

1 Like

oops my bad

time = 0
while true do
time +1
end

is that countdown or second counting?

what i mean is, it goes to 00:00:00 and never ends till someone reaches the winpad, and the timer stops

i will try that if it works brb

will the time be displayed on GUI?

yeah it will. background transparent black, text white and font cartoon

have you scripted the gui yet?

yeah wait i need to go studio brb

local StartTime
local TSeconds = 0
local Minutes = 0
local Seconds = 0
local Mseconds = 0
local Started = false
local GL
local TS=game:GetService’TweenService’
function game.ReplicatedStorage.RequestTime.OnClientInvoke()
return TSeconds,Minutes,Seconds,Mseconds
end
function game.ReplicatedStorage.RequestToolInfo.OnClientInvoke()
return GL
end
function _G:ToggleRestartLabel(v)
local info=TweenInfo.new(v and _G.QuickResetDelay or 3 or .5,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
local tw=TS:Create(script.Parent.Restart,info,{TextTransparency=v and 0 or 1})
tw:Play()
end
game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).ChildAdded:Connect(function(Object)
if Object.Name == “InTower” then
if Started ~= true then
Started = true
GL = {}
StartTime = workspace.DistributedGameTime
local TowerName = game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”):WaitForChild(“InTower”).Value
repeat
wait()
local findgear = game.Players.LocalPlayer.Character:FindFirstChildOfClass(“Tool”)
if findgear then
–if findgear.Name == “Vertical Mobility” or findgear.Name == “Bootleg Coil” then
–for index,value in pairs(GL) do
– if index == findgear.Name then
– print(GL[index])
GL[findgear.Name] = true
– end
–end
–end
end
TSeconds=math.floor(workspace.DistributedGameTime-StartTime)
Minutes = math.floor(math.floor(workspace.DistributedGameTime-StartTime)/(601))
Seconds = math.floor(math.floor(workspace.DistributedGameTime-StartTime)/(1))%(60)
Mseconds = math.floor((workspace.DistributedGameTime-StartTime)
(100))%(100)
script.Parent.TextLabel.Text = string.format(‘%s:%02i.%02i’,Minutes,Seconds,Mseconds)
until game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Health <= 0 or game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”):FindFirstChild(“InTower”) == nil
local Win = game.Players.LocalPlayer.Character:FindFirstChild(“Humanoid”):FindFirstChild(“Win”)
if Win then
if TowerName ~= “ToA” or TowerName ~= “ToM” then
– game.ReplicatedStorage.WinTower:FireServer(“["..game.Players.LocalPlayer.Name.."]:”…" has beaten: “…”"..findabbr(TowerName).."“…” in “…(”"..Minutes..":"..Seconds..":"..Mseconds.."“)…”\n Used VM: “…”“…VM…”“…”\n Used BGC: “…”“…BGC…”")
game.ReplicatedStorage.WinTower:FireServer(TowerName,Minutes,Seconds,Mseconds,GL)
end
end
if game.Players.LocalPlayer.Character:FindFirstChild(“Humanoid”).Health ~= 0 and Win then
–game.ReplicatedStorage.ServerMessage:FireServer(TowerName,Minutes,Seconds,Mseconds)
end
wait(1)
script.Parent.TextLabel.Text = “0:00.00”
Started = false
end
end
end)

ik not well pasted but i tried okay

and yes its a local script, so yeah

local count = 0
while true do
count = count +1
wait(1)
end

Here is your second counter

Easier:

local count = 0
while true do
count += 1
wait(1)
end

oh well i meant it goes from 00:00.00 (first one is minutes, second one is seconds and third one is milliseconds) and it wont stop till the player reaches the winpad (timer starts when player enters tower portal)

Just do some maths based off the counter and here you go.
For hours it is seconds/3600
For minutes it is seconds/60
For miliseconds it is seconds*100

I suggest you use RunService and a variable that holds a timestamp such as os.clock().

local RunService = game:GetService'RunService'
local TextLabel = script.Parent

-- Save a time when you start the tower
local Start = os.clock()

local Timer = RunService.Heartbeat:Connect(function()
	-- The time in seconds that has elapsed since the "Start" time
	local Elapsed = os.clock() - Start
	-- Get the minutes elapsed by dividing the current seconds by 60
	-- and rounding down with math.floor
	local Minutes = math.floor(Elapsed / 60)
	-- Get the seconds elapsed by getting the remainder of a
	-- division by 60 and then rounding it down
	local Seconds = math.floor(Elapsed % 60)
	-- Get the milliseconds elapsed by getting the remainder of a
	-- division by 1 (aka just the decimal number)
	local Milliseconds = (Elapsed % 1) * 100
	-- Since you wanted your time to look like 00:00.00,
	-- the format you want is "%.2d : %.2d . %.2d"
	-- %.2d is just a format that makes a number always have at least 2 digits
	-- (00, 01, 09, etc.)
	TextLabel.Text = string.format("%.2d:%.2d.%.2d", Minutes, Seconds, Milliseconds)
end)

You would obviously need to implement this into your game properly to restart the timer whenever you restart a tower, save the time when you finish a tower, etc.

7 Likes

Yo hey, how can i start the timer Manual and reset it, like for a Race?