FiveMobileFiveMobile

Esta página todavía no está traducida al español. Mostrando la versión en inglés.

Client and server events

Server and client talk through events: one side triggers, the other listens.

Server to client

server.lua:

addCommandHandler("notice", function(player, _, ...)
    local text = table.concat({...}, " ")
    triggerClientEvent(root, "myServer:notice", resourceRoot, text)
end)

client.lua:

addEvent("myServer:notice", true)  -- true = may come from the network
addEventHandler("myServer:notice", resourceRoot, function(text)
    outputChatBox("[NOTICE] " .. text, 255, 80, 80)
end)

Client to server

client.lua:

triggerServerEvent("myServer:buy", resourceRoot, "water")

server.lua:

addEvent("myServer:buy", true)
addEventHandler("myServer:buy", resourceRoot, function(item)
    local player = client  -- who really sent it (don't trust parameters)
    -- validate everything here: does the item exist? can they afford it? are they near the shop?
end)

Security

  • Use client (on the server) to know who triggered the event. Never accept "the player" as a parameter from the client.
  • Validate every value you receive: type, size, and whether it makes sense in the current game state.
  • Prefix your event names (myServer:...) so they don't collide with other resources.