====== Countdown time ======
====Finished Product====
{{ :developer_center:developer_editor:countdown1.mp4 |}}
====Design====
{{ :developer_center:developer_editor:countdown.mp4?900 |}}
====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**:
* **start_button**: This variable is meant to store the identifier for the button that, when clicked, will start the countdown. You need to replace nil with the actual ID assigned to the start button in your UI system.
* **//enter_min//**: This variable will store the ID for the input box where the user enters the minutes. You should replace nil with the actual ID of your minutes input field.
* **//enter_sec//**: This variable will hold the ID for the input box where the user enters the seconds. Similarly, replace nil with the actual ID for this input field.
* **//display_time//**: This variable refers to the ID of the text field where the remaining time will be shown. This ID is specific to the text element in your UI where you want to display the countdown.
* **//uiid//**: This ID represents the entire UI where your elements (buttons, input boxes, text fields) are located. Replace nil with the actual ID for your UI group to ensure the script interacts with the correct UI.
* **//mins and secs//**: These are local variables initialized to 0 to keep track of the minutes and seconds for the countdown. They will be updated based on user input and during the countdown process.
**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)
* Function **//enter_time_to_countdown(e)://**
* **//e//**: This parameter is an event object passed to the function when a UI element triggers an event. It contains information about the event, including the UI element involved and its content.
* **//e.uielement//**: This field holds the ID of the UI element that triggered the event. The function checks if this ID matches **//enter_min//** or **//enter_sec//** to determine which input box the event came from.
* //**e.content**//: This field contains the text input from the UI element. //**tonumber(e.content)**// attempts to convert this text to a number. If the conversion fails (e.g., due to invalid input), **//tonumber//** returns **//nil//**.
**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)://**
* **//e//**: This parameter is an event object passed to the function when a UI button is clicked. It provides details about the button click event, including the ID of the button that was clicked.
* **//if e.uielement == start_button then//**: This condition checks if the button that was clicked is the start button. Only if this condition is true does the countdown logic proceed.
* **//repeat ... until mins == 0 and secs == 0//**: This loop will run until both mins and secs reach 0. It handles the countdown process:
* **//if secs == 0 and mins > 0 then//**: If the seconds are 0 and there are still minutes left **//(mins > 0)//**, it decreases the minutes **//(mins = mins - 1)//** and sets the seconds to **//59//** (since we’re moving to the previous minute).
* **//elseif secs > 0 then//**: If seconds are greater than 0, it simply decrements the seconds by 1 **//(secs = secs - 1)//**.
* **//local formatted_time = string.format("%02d:%02d", mins, secs)://** This line formats the remaining time as a string in //**MM:SS format. %02d**// ensures that both minutes and seconds are always displayed as two digits.
* **//Customui:setText(e.eventobjid, uiid, display_time, formatted_time)//:** Updates the text field with the formatted time. //**e.eventobjid**// is the ID of the event object, **//uiid//** is the UI ID, //**display_time**// is the text field ID, and **//formatted_time//** is the time string to be displayed.
* **//Trigger:wait(1)//:** Pauses execution for 1 second. This simulates the countdown ticking down every second.
* **//Customui:setText(e.eventobjid, uiid, display_time, "00:00")//:** Once the countdown reaches 00:00, the display is set to 00:00 to indicate the end of the countdown.
**Event Registration:**
* **//ScriptSupportEvent:registerEvent("UI.Button.Click", countdown)://** Registers the countdown function to handle the **//"UI.Button.Click"//** event. This means the function will be called when the start button is clicked, triggering the countdown process.
----
If you have any questions about Developer Tools, feel free to join our official discord server
[[https://discord.gg/NVRZHBChBt|Mini World Global DEV discord]]