Skip to content

tw.waiter

Side: Shared (Client + Server)

Utility for waiting until a condition becomes true, with timeout support.

Functions

tw.waiter.exec(condition, on_tick?, poll_interval?, timeout_ms?)

Blocks the current thread until condition() returns true, or until the timeout is reached.

ParameterTypeDefaultDescription
conditionfunctionFunction called repeatedly. Must return true to stop waiting
on_tickfunction?nilOptional function called each tick while waiting
poll_intervalinteger?0Milliseconds between each condition check
timeout_msinteger?5000Maximum wait time in milliseconds. 0 = no timeout

Returns: booleantrue if the condition was met, false if the timeout was reached.

Examples

Wait for a model to load

lua
RequestModel(model_hash)
local loaded = tw.waiter.exec(function()
    return HasModelLoaded(model_hash)
end)

if not loaded then
    print("Model failed to load within 5 seconds")
end

Custom timeout and poll interval

lua
-- Check every 100ms, timeout after 10 seconds
local ready = tw.waiter.exec(function()
    return IsPlayerReady()
end, nil, 100, 10000)

With on_tick callback

lua
-- Draw text while waiting for a door to open
local opened = tw.waiter.exec(function()
    return IsDoorOpen(door_id)
end, function()
    tw.text3d.draw(door_coords, "Opening...")
end)

No timeout (wait forever)

lua
-- Wait indefinitely until the condition is true
tw.waiter.exec(function()
    return HasCollisionLoadedAroundEntity(entity)
end, nil, 0, 0)

Notes

  • Default timeout is 5000ms (5 seconds). If the condition is not met within this time, the function returns false.
  • Set timeout_ms to 0 to wait indefinitely (use with caution).
  • The on_tick callback is useful for drawing UI elements (text, markers) while waiting.

Premium RedM Scripts — Multi-Framework