Hello, i am trying to make a Host Timer system, like in talent show.
I already have a script that shows username when joined the host team. How do i make a 30 Minutes timer that shows on text label time 00:00? Seconds:Minutes
-- Server Script
game.ReplicatedStorage.JoinHostTeam.OnServerEvent:Connect(function(Player)
for i,plr in pairs(game:GetService("Players"):GetPlayers())do
plr.PlayerGui:WaitForChild("HostTimerGui").Frame.Playername.Text = "" ..Player.Name
plr.PlayerGui:WaitForChild("HostTimerGui").Frame.ImageLabel.Image = "https://www.roblox.com/headshot-thumbnail/image?userId="..Player.UserId.."&width=420&height=420&format=png"
end
end)
function updateUI(timeLeft)
... -- Code to display this information to the users, maybe remote events?
end
local hostTimeLeft = 1800 -- 30 minutes (?)
repeat
hostTimeLeft -= task.wait(1)
updateUI(hostTimeLeft)
until hostTimeLeft <= 0
print("Times up!")
You have the total amount of seconds, extract the minutes by doing minutes = timeLeft / 60 and round it up down (as we are only interested in whole minutes remaining). Subtract minutes * 60 from timeLeft and you will get the remaining seconds.
function updateUI(timeLeft)
local minutes = math.floor(timeLeft / 60)
if minutes < 10 then
minutes = "0" .. tostring(minutes) -- Add a 0 before the number (prevent display such as '3:30'
end
local seconds = math.round(timeLeft - minutes * 60)
if seconds < 10 then
seconds = "0" .. tostring(seconds) -- Same as above, prevent things such as '30:0'
end
local text = string.format("%s:%s", tostring(minutes), tostring(seconds))
print(text)
end
updateUI(80) -- Prints '01:20'
updateUI(1800) -- Prints '30:00'
local function Timer(Time)
local Minutes = math.floor(Time / 60)
local Seconds = Time - (Minutes * 60)
return string.format("%02d:%02d", Minutes, Seconds)
end
print(Timer(80)) --01:20
print(Timer(1800)) --30:00
ā%dā is the specifier (used to format integers), ā0ā is the flag (fills unused space with zeros instead of whitespaces) and ā2ā is the width (the width of the output value).