Skip to content

Timer UI Module โ€‹

The Timer UI module provides a native-style countdown timer for RedM that appears on screen.

Getting Started โ€‹

lua
local Timer = exports.bln_lib:timer()

Basic Usage โ€‹

Start a Timer โ€‹

lua
-- Start a simple 60-second timer
Timer.start(60)

-- Start a timer with low time warning at 10 seconds
Timer.start(60, 10)

-- Start a timer with callback when finished
Timer.start(
    60,               -- Duration in seconds
    10,               -- Low time warning at 10 seconds
    function()        -- Callback when timer ends
        print("Timer completed!")
    end
)

Stop a Timer โ€‹

lua
-- Stop the timer manually
Timer.stop()

Methods โ€‹

Timer.start(time, lowTime, callback) โ€‹

Start a countdown timer.

Parameters

  • time (number): Duration in seconds
  • lowTime (number, optional): Threshold for "low time" warning (default: 10)
  • callback (function, optional): Function to call when timer completes

Example

lua
-- 2-minute timer with warning at 30 seconds
Timer.start(120, 30, function()
    -- Timer completed
    DoSomething()
end)

Timer.stop() โ€‹

Stop the active timer and remove it from the screen.

Example

lua
-- Stop timer early
Timer.stop()

Visual Elements โ€‹

The timer appears in the native RedM style:

  • Timer displays as "MM:SS" format (e.g., "01:30")
  • Timer turns red when reaching the "low time" threshold
  • Uses native UI flow blocks for authentic game appearance

Examples โ€‹

Mission Timer โ€‹

lua
-- Start mission with 5-minute timer
function StartMission()
    -- Initialize mission
    SpawnEnemies()
    
    -- Start 5-minute timer
    Timer.start(
        300,           -- 5 minutes
        60,            -- Warning at 1 minute left
        function()
            -- Time's up, mission failed
            FailMission("Time expired")
        end
    )
end

-- Complete mission and stop timer
function CompleteMission()
    Timer.stop()
    GiveReward()
end

Cooking Timer โ€‹

lua
-- Start cooking process
function StartCooking(recipe)
    local cookTime = recipe.cookTime
    
    -- Display cooking timer
    Timer.start(
        cookTime,
        math.min(10, cookTime / 3),  -- Warning at 1/3 of cook time or 10 seconds, whichever is less
        function()
            -- Food is ready
            NotifyPlayer("Your food is ready!")
        end
    )
end

Event Countdown โ€‹

lua
-- Start countdown to event
function CountdownToEvent(seconds)
    Timer.start(
        seconds,
        30,
        function()
            StartEvent()
        end
    )
end

Best Practices โ€‹

  1. Clear Timer on Script Stop
lua
AddEventHandler('onResourceStop', function(resourceName)
    if GetCurrentResourceName() == resourceName then
        Timer.stop()
    end
end)
  1. Only One Timer at a Time The module supports only one active timer at a time. Starting a new timer will replace any existing one.

  2. Reasonable Time Values While the timer can technically handle very large values, it's best to keep durations reasonable (under 1 hour) for usability.