And if you instead meant a timer that displays the length of time the player has been in the server (which counts upwards from zero), then here you go.
local label = script.Parent
local currentTime = 0
local function formatTime()
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = (currentTime - (hours * 3600) - (minutes * 60))
local format = "%02d:%02d:%02d"
return format:format(hours, minutes, seconds)
end
local function startTimer()
while true do
currentTime += 1
label.Text = formatTime()
task.wait(1)
end
end
startTimer()
local label = script.Parent
local function formatTime(currentTime)
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = (currentTime - (hours * 3600) - (minutes * 60))
local format = "%02d:%02d:%02d"
return format:format(hours, minutes, seconds)
end
local function startTimer(currentTime)
while true do
currentTime -= 1
label.Text = formatTime(currentTime)
task.wait(1)
end
end
startTimer(5000) --Change this to specify where the timer should start from.
Here’s a server script which works with a SurfaceGui instance.
local run = game:GetService("RunService")
local label = script.Parent
local function formatTime(currentTime)
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
local milliseconds = 1000 * (currentTime - math.floor(currentTime))
local format = "%02d:%02d:%02d:%03d"
return format:format(hours, minutes, seconds, milliseconds)
end
local function startTimer(currentTime)
run.Stepped:Connect(function(elapsed, step)
currentTime -= step
label.Text = formatTime(currentTime)
end)
end
startTimer(5000)
local run = game:GetService("RunService")
local sound = script.Sound --Put a sound instance inside the script named "Sound".
local label = script.Parent
local oldElapsed = 0
local function formatTime(currentTime)
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
local milliseconds = 1000 * (currentTime - math.floor(currentTime))
local format = "%02d:%02d:%02d:%03d"
return format:format(hours, minutes, seconds, milliseconds)
end
local function startTimer(currentTime)
run.Stepped:Connect(function(newElapsed, step)
if oldElapsed ~= newElapsed then
sound:Play()
end
oldElapsed = newElapsed
currentTime -= step
label.Text = formatTime(currentTime)
end)
end
startTimer(5000)
I’ve added an instruction as a comment to the script.
local run = game:GetService("RunService")
local sound = script.Sound --Put a sound instance inside the script named "Sound".
local label = script.Parent
local oldElapsed = 0
local function formatTime(currentTime)
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
local milliseconds = 1000 * (currentTime - math.floor(currentTime))
local format = "%02d:%02d:%02d:%03d"
return format:format(hours, minutes, seconds, milliseconds)
end
local function startTimer(currentTime)
run.Stepped:Connect(function(newElapsed, step)
if oldElapsed ~= newElapsed then
sound:Play()
end
oldElapsed = newElapsed
currentTime -= step
if currentTime <= 0 then
label.Text = "Timer has finished!"
return
end
label.Text = formatTime(currentTime)
end)
end
startTimer(5)
Probably because the length of your sound is more than one second and it’s attempting to play the sound each second, I’d recommend just removing that part of the script.
local run = game:GetService("RunService")
local sound = script.Sound --Put a sound instance inside the script named "Sound".
local label = script.Parent
local oldElapsed = 0
local function formatTime(currentTime)
local hours = math.floor(currentTime / 3600)
local minutes = math.floor((currentTime - (hours * 3600))/60)
local seconds = math.floor((currentTime - (hours * 3600) - (minutes * 60)))
local milliseconds = 1000 * (currentTime - math.floor(currentTime))
local format = "%02d:%02d:%02d:%03d"
return format:format(hours, minutes, seconds, milliseconds)
end
local function startTimer(currentTime)
run.Stepped:Connect(function(newElapsed, step)
if oldElapsed ~= math.floor(newElapsed) then
sound:Play()
end
oldElapsed = math.floor(newElapsed)
currentTime -= step
if currentTime <= 0 then
label.Text = "Timer has finished!"
return
end
label.Text = formatTime(currentTime)
end)
end
startTimer(10)