What is the best way to make a minutes : seconds timer?

Hello, I’m Mario and I’m currently working with my team. My friend and I(the scripters) are making the round system but we don’t know the best way to make a timer that can be converted into a function that displays minutes and seconds. We currently think repeat until is the best way to make it. I’m open to any suggestions, thanks for helping!

1 Like

You can use some remainder math to extract the seconds value

function timer(Time)
    local seconds = Time % 60
    local hours = (Time - seconds) / 60
    return hours, seconds
end
5 Likes

I have a game that uses something similar but for money:

image

The way I accomplished this is by handling the loop itself on the server. From there I can hook a callback function to be executed every second of the loop that then lets me update the GUI.

Yours is a bit different though since you need a game loop not individual loops for each player. The way you could do this is like so:

local roundTime = 300; -- 5 minutes 
while wait(1) do
  if roundTime <= 0 then
    roundTime = 300; -- reset the timer
    -- do other game stuff in here once it reaches 0
  end
  updateClient:FireAllClients(roundTime); -- update all the client timer GUIs
  roundTime -= 1;
end

Then on your clients:

updateClient.OnClientEvent:Connect(function(roundTime)
  local minutes = roundTime / 60;
  minutes = minutes - (math.fmod(minutes, 1));
  local seconds = roundTime - (minutes * 60);
  -- you now have your minutes and seconds just format them
  guiTextLabel.Text = tostring(minutes) .. ":" .. tostring(seconds)
end)
1 Like

You might be able to use some sort of event that sends a signal to start and stop a timer on the client.
To calculate the time, you can just use a while loop to keep adding 1 to the seconds value.

There’s also the option of setting an intvalue and using a GetPropertyChangedSignal to change it on the client.

1 Like

This can work with a for loop timer, thanks!