Almost all communication between users is done through the commands system.
local onlinePlay = require("scripts/onlinePlay")
-- A command can be created using onlinePlay.createCommand. A unique name and an importance value is given.
local exampleCommand = onlinePlay.createCommand("example", onlinePlay.IMPORTANCE_MAJOR)
-- You can specify what should happen when a command is received by using its onReceive function.
function exampleCommand.onReceive(sourcePlayerIdx, foo,bar)
-- Your code here.
end
-- You can then send a command like so.
-- The first argument is the index of the player that it will be sent to (0 to send to everyone).
-- Anything after will be directly passed into the onReceive function.
exampleCommand:send(0, foo,bar)
The code comments here mostly explain how this system works. When creating a command, it is important that the name given to it is unique. As such, you should try to avoid any overly generic names.
The importance value for a command can be onlinePlay.IMPORTANCE_MINOR, onlinePlay.IMPORTANCE_MAJOR, or onlinePlay.IMPORTANCE_VITAL. Minor should only be used for small, continuous updates, and there is no guarantee that the messages arrive in order or even arrive at all.
Major should be used for most events, as it is essentially guaranteed that it will arrive. It is also always processed in order of when it was sent. Vital is very niche and you should never really need it. It is the same as major, but it will freeze the game until it has arrived.
While any basic Lua value can be used as an argument to Command:send, more complex types (like NPCs) cannot be sent. See the encoding page for more.
There is a version of the commands system specifically for NPCs. See the NPCs page for more.