Table of Contents

Countdown time

Finished Product

Design

Script

local start_button = nil -- Replace this with your Start Button ID 
local enter_min = nil -- Replace this with your input box ID for minutes 
local enter_sec = nil -- Replace this with your input box ID for seconds 
local display_time = nil -- Replace this with your display text ID here
local uiid = nil -- Replace this with your UI ID here
 
local mins = 0
local secs = 0
 
-- Function to handle time input from UI elements
function enter_time_to_countdown(e) 
    if e.uielement == enter_min then
        mins = tonumber(e.content)
    end
 
    if e.uielement == enter_sec then
        secs = tonumber(e.content) 
    end
end
 
-- Register the time input handling function for when the UI element loses focus
ScriptSupportEvent:registerEvent("UI.LostFocus", enter_time_to_countdown)
 
-- Function to start and handle the countdown process
function countdown(e)
    if e.uielement == start_button then
        repeat
            -- Handle countdown logic
            if secs == 0 and mins > 0 then 
                mins = mins - 1
                secs = 59
            elseif secs > 0 then
                secs = secs - 1
            end
 
            -- Update the countdown display
            local formatted_time = string.format("%02d:%02d", mins, secs)
            Customui:setText(e.eventobjid, uiid, display_time, formatted_time)
 
            -- Wait for 1 second before the next update
            Trigger:wait(1)
        until mins == 0 and secs == 0
        Customui:setText(e.eventobjid, uiid, display_time, "00:00") 
    end
end
 
-- Register the countdown function for when the UI button is clicked
ScriptSupportEvent:registerEvent("UI.Button.Click", countdown)

Explanation

Variable Initialization

local start_button = nil 
local enter_min = nil
local enter_sec = nil 
local display_time = nil 
local uiid = nil 
 
local mins = 0
local secs = 0

Variables:

Handling Time Input

function enter_time_to_countdown(e) 
    if e.uielement == enter_min then
        mins = tonumber(e.content) 
    end
 
    if e.uielement == enter_sec then
        secs = tonumber(e.content) 
    end
end
 
ScriptSupportEvent:registerEvent("UI.LostFocus", enter_time_to_countdown)

Countdown Function

function countdown(e)
    if e.uielement == start_button then
        repeat
            if secs == 0 and mins > 0 then 
                mins = mins - 1
                secs = 59
            elseif secs > 0 then
                secs = secs - 1
            end
 
            local formatted_time = string.format("%02d:%02d", mins, secs)
            Customui:setText(e.eventobjid, uiid, display_time, formatted_time)
 
            Trigger:wait(1)
        until mins == 0 and secs == 0
        Customui:setText(e.eventobjid, uiid, display_time, "00:00") -- Reset display to 00:00
    end
end
 
ScriptSupportEvent:registerEvent("UI.Button.Click", countdown)

Function countdown(e):

Event Registration:


If you have any questions about Developer Tools, feel free to join our official discord server Mini World Global DEV discord