Table of Contents

Timer

Advanced Document: Countdown time

Finished products

Design

Diagram

Script

local uiid = nil --- replace your UI ID here 
local text_timer = nil --- replace you text here 
local min = nil --- replace your minutes here 
local sec = nil --- replace your seconds here 

function game(e)
    repeat
        sec = sec - 1 
        Customui:setText(e.eventobjid, uiid,text_timer, min .. ":" .. sec)
        if sec == 0 and min > 0 then 
            sec = 60 
            min = min -1 
            Customui:setText(e.eventobjid, uiid,text_timer, min .. ":" .. sec)
        end
	Trigger:wait(1)
    until min == 0 and sec == 0
end

ScriptSupportEvent:registerEvent("UI.Show", game)

Explanation

Variable Declarations

local uiid = nil --- replace your UI ID here 
local text_timer = nil --- replace you text here 
local min = nil --- replace your minutes here 
local sec = nil --- replace your seconds here 

Game function

function game(e)
    repeat
        sec = sec - 1 
        Customui:setText(e.eventobjid, uiid, text_timer, min .. ":" .. sec)
        if sec == 0 and min > 0 then 
            sec = 60 
            min = min - 1 
            Customui:setText(e.eventobjid, uiid, text_timer, min .. ":" .. sec)
        end
	Trigger:wait(1)
    until min == 0 and sec == 0
end

Within the loop:

The loop continues to execute until both min and sec are 0.

Event Registration

  ScriptSupportEvent:registerEvent("UI.Show", game)

ScriptSupportEvent:registerEvent(“UI.Show”, game): Registers the game function to be called when the “UI.Show” event occurs. This means the countdown will start whenever the UI is shown.

Summary

In summary, this script sets up a countdown timer that updates a specified UI element every second. It decreases the seconds and minutes accordingly and updates the UI text to reflect the current countdown time. When the countdown reaches zero minutes and zero seconds, the loop stops. The timer starts when the “UI.Show” event is triggered.


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