> ## Documentation Index
> Fetch the complete documentation index at: https://zyra-store.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Server events

**Server events** are messages that Zyra Store scripts emit in the FiveM server environment. Unlike client events, these are processed on your server and allow you to react to player actions, data synchronizations, or administrative events from your server's central logic.

To subscribe to any Zyra Store server event, use `AddEventHandler` from a **server-side** file in your resource. The event name always follows the pattern `zyra:sv_eventName`.

An important particularity of server events in FiveM is the global **`source`** variable, which is automatically available inside the callback and contains the server ID of the player who originated the event. Always use it to identify and validate the involved player.

```lua other-resource/server.lua theme={null}
AddEventHandler('zyra:sv_onPlayerAction', function(source, action, data)
    local playerId = source
    print(('[Zyra] Player %s performed action: %s'):format(playerId, action))
end)
```

***

## Event reference

> [accordion] zyra:sv\_onPlayerConnect
>
> Fires when a player's data has been correctly registered on the server by the script. It is the right time to initialize information for that player in your own systems.
>
> **Parameters**
>
> **source** `number` *required*
>
> The FiveM server ID of the player who connected. Use it to identify the player in all native server calls.
>
> **identifier** `string` *required*
>
> The player's unique license identifier (for example, `license:a1b2c3d4...`). Use it to associate the player with records in your database.
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onPlayerConnect', function(source, identifier)
>     print(('[MyResource] Player %s connected with identifier: %s'):format(source, identifier))
>     -- Initialize player data in your own resource
>     MyResource.InitializePlayer(source, identifier)
> end)
> ```

> [accordion] zyra:sv\_onPlayerAction
>
> Fires on the server every time a player performs an action tracked by the script. Use it to log actions, validate operations, or apply server-side business logic.
>
> **Parameters**
>
> **source** `number` *required*
>
> The FiveM server ID of the player who performed the action.
>
> **action** `string` *required*
>
> The name of the performed action, for example `"purchase"`, `"interact"`, or `"use_item"`.
>
> **data** `table` *required*
>
> A table with information specific to the action. The content varies depending on the `action` value.
>
> **Common data structure**
>
> **timestamp** `number`
>
> Unix timestamp of the moment the action occurred on the client.
>
> **metadata** `table`
>
> Additional optional data that the script attaches depending on the action type.
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onPlayerAction', function(source, action, data)
>     -- Log the action in a server log
>     local playerName = GetPlayerName(source)
>     print(('[MyResource] %s (ID: %s) performed: %s'):format(playerName, source, action))
>
>     -- Validate that the action is allowed for this player
>     if action == 'purchase' and not MyResource.HasPermission(source, 'buy') then
>         print(('[MyResource] WARNING: Player %s attempted an action without permission.'):format(source))
>         return
>     end
>
>     -- Process the validated action
>     MyResource.LogAction(source, action, data)
> end)
> ```

> [accordion] zyra:sv\_onAdminAction
>
> Fires when an administrator executes a privileged command through the script. Ideal for building audit systems or logging sensitive administrative actions.
>
> **Parameters**
>
> **source** `number` *required*
>
> The server ID of the administrator who executed the command.
>
> **command** `string` *required*
>
> The name of the executed administrative command (for example, `"kick"`, `"ban"`, `"give_item"`).
>
> **target** `number | nil`
>
> The server ID of the player target of the command, if applicable. Will be `nil` if the command has no player target.
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onAdminAction', function(source, command, target)
>     local adminName = GetPlayerName(source)
>     local targetInfo = target and ('on player ID ' .. target) or '(no target)'
>
>     -- Log in the audit log
>     local logEntry = ('[AUDIT] Admin %s (ID: %s) executed "%s" %s'):format(
>         adminName, source, command, targetInfo
>     )
>     print(logEntry)
>     MyResource.SaveAudit(source, command, target, os.time())
> end)
> ```

> [accordion] zyra:sv\_onDataSync
>
> Fires when the script synchronizes a player's data across the server. You can use this event to keep your own systems updated with the player's most recent data.
>
> **Parameters**
>
> **source** `number` *required*
>
> The FiveM server ID of the player whose data is being synchronized.
>
> **data** `table` *required*
>
> The synchronized data object. Contains the updated player information as managed internally by the script.
>
> **Common data object fields**
>
> **identifier** `string`
>
> Player's license identifier.
>
> **lastSync** `number`
>
> Unix timestamp of the last successful synchronization.
>
> **payload** `table`
>
> The specific synchronized data. The structure depends on the script configuration.
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onDataSync', function(source, data)
>     print(('[MyResource] Synchronizing player %s data'):format(source))
>
>     -- Update your resource's local cache with the new data
>     if data and data.payload then
>         MyResource.UpdateCache(source, data.payload)
>     end
> end)
> ```

> [accordion] zyra:sv\_onResourceStart
>
> Fires when the Zyra Store script resource starts correctly. It is the ideal time to initialize database connections, caches, or data structures that your integration needs.
>
> **Parameters**
>
> This event receives no action parameters. In the context of server events without an originating player, `source` will have the value `0` (which represents the server itself).
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onResourceStart', function()
>     -- source == 0 indicates the sender is the server, not a player
>     print('[MyResource] The Zyra Store script has started.')
>
>     -- Initialize your database or cache
>     MyResource.ConnectDatabase()
>     MyResource.LoadConfiguration()
>
>     print('[MyResource] Initialization completed, ready to operate.')
> end)
> ```

> [accordion] zyra:sv\_onResourceStop
>
> Fires when the Zyra Store script resource is about to stop. Take advantage of this event to save pending data, clean up active timers, and release resources correctly.
>
> **Parameters**
>
> This event receives no parameters. Like `zyra:sv_onResourceStart`, `source` will be `0`.
>
> **Example**
>
> ```lua other-resource/server.lua theme={null}
> AddEventHandler('zyra:sv_onResourceStop', function()
>     print('[MyResource] The Zyra Store script is stopping...')
>
>     -- Save all pending data before the resource stops
>     MyResource.SavePendingData()
>
>     -- Clean up active timers or intervals
>     if MyResource.syncTimer then
>         ClearInterval(MyResource.syncTimer)
>         MyResource.syncTimer = nil
>     end
>
>     print('[MyResource] Cleanup completed.')
> end)
> ```

***

## Security in server events

Server events are the core of your application's logic and must be handled with care. Follow these practices to keep your server secure:

**Always validate `source`.** Never blindly trust the player ID you receive. Verify that the player exists and has the necessary permissions before processing any action.

```lua other-resource/server.lua theme={null}
AddEventHandler('zyra:sv_onPlayerAction', function(source, action, data)
    -- Verify that the player is valid
    if not GetPlayerName(source) then
        print('[MyResource] ALERT: invalid source received: ' .. tostring(source))
        return
    end

    -- Continue with secure processing
    MyResource.ProcessAction(source, action, data)
end)
```

**Never trust data coming from the client.** Although Zyra Store server events are emitted internally by the script, any data that eventually comes from the client must be validated and sanitized before being used in database queries or other critical operations.

**Verify permissions before processing administrative actions.** Even if the `zyra:sv_onAdminAction` event was fired by the script, check that the player indicated in `source` really has administrator permissions in your systems.

> [!WARNING]
>
> **Important:** Always verify the administrator's permissions on the server before processing or confirming an administrative action. It is not enough that the event is fired by the script; a malicious actor could attempt to manipulate the flow through other resources. Your server is the last line of defense.

***

## Related resources

* See the [Client Events](https://zyra-store.mintlify.site/en/reference/events/client-events.md) for events emitted in the player environment.
* Review the [Server Exports](https://zyra-store.mintlify.site/en/reference/exports/server-exports.md) for functions you can call directly from your resource without needing to listen to events.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.