- Documentation
- Shared functions
- waitForEvent
Function
waitForEvent
Pauses an async task until an event happens. Returns the event's source element and its arguments. With a time limit it returns false if the event did not happen in time. The handler and the limit timer are removed when the wait ends.
Syntax
element, ... waitForEvent(string eventName [, element attachedTo = root, int timeoutMs = nil])Arguments
eventNamestring- The event name.
attachedToelement, optional = root- The element to listen on (with its children).
timeoutMsint, optional = nil- Time limit in ms; without it waits forever.
Returns
The source and the event arguments, or false on timeout.
Example
addCommandHandler("wyzwanie", function(player)
async(function()
outputChatBox("Masz 10 sekund, żeby wejść do auta!", player)
local who = waitForEvent("onPlayerVehicleEnter", player, 10000)
if who then
outputChatBox("Udało się!", player)
else
outputChatBox("Za późno.", player)
end
end)
end)
See also
asyncRuns a function as a task (coroutine). The code in the task runs immediately until its first wait (sleep,await,waitForEvent). Then it yields and the server and the game keep running. When the wait is over, the task continues from where it stopped. This lets you write steps spread over time (a countdown, a database query, an HTTP response, waiting for a player) one after another, without nested callbacks and timers.addEventHandlerAttaches a function to an event. This is how you react to a player joining, dying, a key press, every frame and your own events.triggerEventTriggers an event on an element on the same side (server → server, client → client). The handlers run immediately, before the function returns. The event must be registered withaddEventor be built in.