Advertisement
Remusutules

Countdown TImer Module

Nov 28th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | Source Code | 0 0
  1. local CountdownTimer = {}
  2. local module = script
  3.  
  4.  
  5. local function formatTime(minutes, seconds)
  6.     return string.format("%02d:%02d", minutes, seconds)
  7. end
  8.  
  9. function CountdownTimer.startCountdown(Minutes,Seconds)
  10.     local minutes = Instance.new("IntValue",module) or module:WaitForChild("Minutes")
  11.     minutes.Name = "Minutes"
  12.     minutes.Value = tonumber(Minutes)
  13.     local seconds = Instance.new("IntValue",module) or module:WaitForChild("Seconds")
  14.     minutes.Name = "Seconds"
  15.     minutes.Value = tonumber(Seconds)
  16.     local player = game.Players.LocalPlayer
  17.     local playerGui = player:WaitForChild("PlayerGui")
  18.     local textLabel = playerGui:WaitForChild("CountdownTimer").MainFrame:WaitForChild("CountdownLabel")
  19.  
  20.     local function updateText()
  21.         if seconds.Value == 0 and minutes.Value > 0 then
  22.             minutes.Value = minutes.Value - 1
  23.             seconds.Value = 59
  24.         end
  25.  
  26.         if seconds.Value <= 9 then
  27.             textLabel.Text = string.format("%02d:0%d", minutes.Value, seconds.Value)
  28.         else
  29.             textLabel.Text = string.format("%02d:%02d", minutes.Value, seconds.Value)
  30.         end
  31.     end
  32.  
  33.     updateText()
  34.  
  35.     minutes:GetPropertyChangedSignal("Value"):Connect(updateText)
  36.     seconds:GetPropertyChangedSignal("Value"):Connect(updateText)
  37.  
  38.     spawn(function()
  39.         while minutes.Value > 0 or seconds.Value > 0 do
  40.             wait(1)
  41.             if seconds.Value == 0 then
  42.                 if minutes.Value > 0 then
  43.                     minutes.Value = minutes.Value - 1
  44.                     seconds.Value = 59
  45.                 else
  46.                     break
  47.                 end
  48.             else
  49.                 seconds.Value = seconds.Value - 1
  50.             end
  51.             updateText()
  52.         end
  53.         textLabel.Text = "00:00"
  54.     end)
  55. end
  56. f
  57. return CountdownTimer
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement