Skip to content

tm.loop ​

Managed repeating loops: a thread that runs action while condition holds, with a stop handle. Shared.

lua
local id = tm.loop.new({
    condition = function() return IsPedInAnyVehicle(PlayerPedId(), false) end,
    action = function()
        -- work
        return nearTarget and 0 or 500    -- optional: the wait before the next pass, in ms
    end,
    interval = 500,                       -- default wait between passes, in ms
})

tm.loop.remove(id)                        -- stops it
FunctionNotes
new({ condition, action, interval })Starts the loop and returns its id. All three fields are required (an error otherwise). The loop ends the first time condition returns false or nil.
remove(id)Stops a loop. Returns false for an unknown id or a loop that already ended on its own. A loop can remove itself from its own action.

If action returns a number it replaces interval for that pass, so one loop can run fast near a target and slow away from it without a raw CreateThread. Keep interval as long as the feature allows, because it is the idle cost.

A loop whose condition or action errors logs the error through tm.log.error and stops, so a bug cannot spam the console every tick.