Getting Started with Events
RAGE:MP Documentation
Events System
Events are the backbone of scripting in RAGE:MP. This page explains what they are, how to register them, how communication works between server, client, and CEF, and how to manage them cleanly.
Introduction
Events are the core scripting system for RAGE:MP.
They are registered by you and triggered when a specific action or situation happens.
For example, PlayerEnterCheckpoint is triggered when a player enters a checkpoint. That event gives you:
- the Player that entered it
- the Checkpoint that was entered
Event Types
These are provided by RAGE:MP and are triggered automatically in certain situations.
- player entering a checkpoint
- player quitting
- player spawning
These are created by you and can be triggered whenever you want.
- connect systems together
- trigger your own gameplay logic
- send data between layers
- create reusable scripting flows
Registering Events
Important: You must register an event before using it anywhere.
Server-side Event
A normal server-side event can be registered like this:
mp.events.add('eventName', (arg1, arg2) => {
// code
});
Client → Server Event
If the event is triggered from client-side but registered on server-side, the first argument must be player.
This happens because the client sends the local player together with the event data, allowing the server to know who triggered it.
mp.events.add('eventName', (player, arg1, arg2) => {
// code
});
CEF Notes
- CEF cannot communicate directly with server-side
- CEF mainly communicates with client-side
- Older documentation often describes CEF more restrictively, but newer support exists in later versions
Added: CEF: mp.events.add(string eventName, function handler) Added: CEF: mp.events.reset() Added: CEF: mp.events.remove(string eventName) Added: CEF: mp.events.call(string eventName) (alias to mp.trigger) Added: Client-side: Browser.call(eventName, arguments...)
Calling Events
Tip: Most event confusion comes from not knowing which side is calling which side.
JavaScript Syntax
mp.events.call('eventName', args);
JavaScript Syntax
player.call('eventName', [arg1, arg2]);
JavaScript Syntax
mp.players.call('eventName', [arg1, arg2]);
JavaScript Syntax
mp.players.call(
mp.players.toArray().filter((_player) => _player.name == 'WeirdNewbie'),
'eventName',
[args]
);
JavaScript Syntax
mp.events.callRemote('eventName', args);
JavaScript Syntax
mbrowser.execute(`javascriptFunction('${variable1}', '${variable2}');`);
JavaScript Syntax
mp.trigger('eventName', args);
Example Flow
In this example, a player uses a server command, and the server tells that player's client to start a screen effect.
mp.events.addCommand('effect', (player, fullText, effect) => {
player.call('startEffectEvent', [effect]);
});
mp.events.add('startEffectEvent', (effect) => {
mp.game.graphics.startScreenEffect(effect, 10000, false);
});
Cancelling and Managing Events
To control an event instance directly, you can use mp.Event.
let ev = new mp.Event("playerDeath", (player, reason, killer) =>
{
mp.players.broadcast('First blood!');
ev.destroy(); // this event handler will not be called anymore
});
// ev.destroy(); // destroy it before it can execute again
You can remove a specific handler, remove all handlers for an event, remove multiple events, or reset the whole tree.
// Remove specified handler of specified event
function playerJoinHandler(player)
{
}
mp.events.add("playerJoin", playerJoinHandler);
mp.events.remove("playerJoin", playerJoinHandler);
// Remove handler(s) of specified event(s)
mp.events.remove("playerJoin");
mp.events.remove(["playerJoin", "playerQuit"]);
// Reset whole event tree
mp.events.reset();
// Get all handlers of specified event
mp.events.getAllOf("playerJoin").forEach(_ev => _ev(null));
Quick Reference
Summary
- Events are the foundation of scripting in RAGE:MP
- Built-in events are automatic, custom events are created by you
- Always register events before using them
- Server, client, and CEF each have different communication rules
- You can disable, destroy, remove, or reset events when needed