- Documentation
- Server functions
- triggerClientEvent
Function
triggerClientEvent
Sends an event from the server to one player, several players or everyone at once.
Syntax
bool triggerClientEvent([table|player targets,] string name, element source [, ...])Arguments
targetsplayer|table, optional- A player or a table of players. If you omit it or pass
root, the event goes to everyone. namestring- Event name. The client must register it with
addEvent(name, true). sourceelement- The element the client will see as
source. ...any, optional- Arguments for the handler: numbers, strings, tables and elements.
Returns
true if the event was sent.
Example
The server shows a message in the middle of the player's screen. The client does the drawing.
function showMessage(player, text)
triggerClientEvent(player, "showMessage", resourceRoot, text)
end
addCommandHandler("hello", function(player)
showMessage(player, "Welcome to Los Santos!")
end)
addEvent("showMessage", true)
local text, untilTick = nil, 0
local sw, sh = guiGetScreenSize()
addEventHandler("showMessage", resourceRoot, function(t)
text, untilTick = t, getTickCount() + 4000
end)
addEventHandler("onClientRender", root, function()
if text and getTickCount() < untilTick then
dxDrawText(text, 0, 0, sw, sh * 0.4, tocolor(255, 255, 255), 2, "pricedown", "center", "bottom")
end
end)
Notes
Good to know. The short form without targets also works: triggerClientEvent("name", source, ...) sends to all players.
Send only what you need. Every call goes over the network. Instead of sending the same thing every frame, send it once and let the client draw.