Skip to main content

Introduction

Callbacks in RSG Core provide a way to request data from the server and wait for a response, or vice versa. Theyโ€™re essential for synchronous communication between client and server scripts. Location: resources/[framework]/rsg-core/server/functions.lua & client/functions.lua
Callbacks are perfect for situations where you need to wait for data before continuing execution, like checking if a player has an item, getting database information, or validating actions.

Core Features

๐Ÿ“ก Bidirectional Communication

  • Server Callbacks: Client requests data from server
  • Client Callbacks: Server requests data from specific client
  • Multiple Arguments: Pass any number of arguments
  • Any Data Types: Return tables, strings, numbers, booleans

๐Ÿ”’ Secure by Design

  • Server-side Validation: All sensitive logic stays on server
  • No Client Trust: Client only receives processed results
  • Source Tracking: Always know which player triggered callback

โšก Efficient Data Flow

  • Async Processing: Non-blocking operations
  • Promise Support: Await pattern for cleaner code
  • No Event Spam: Request-response pattern prevents loops

Server Callbacks

Server callbacks allow you to request data from the server and wait for the response on the client.

Creating a Server Callback

Use RSGCore.Functions.CreateCallback on the server to create a callback:
Parameters:
  • source: The player who triggered the callback
  • cb: The callback function to send data back
  • ...: Any additional arguments passed from the client

Triggering a Server Callback

Use RSGCore.Functions.TriggerCallback on the client to call the callback:

Client Callbacks

Client callbacks allow the server to request data from a specific client and wait for the response.

Creating a Client Callback

Use RSGCore.Functions.CreateClientCallback on the client to create a callback:

Triggering a Client Callback

Use RSGCore.Functions.TriggerClientCallback on the server to call the callback:

Practical Examples

Example 1: Checking if Player Has Item

Server Callback:
Client Usage:

Example 2: Getting Playerโ€™s Horses

Server Callback with Database:
Client Usage:

Example 3: Purchasing Item

Server Callback:
Client Usage:

Example 4: Client Callback for Coordinates

Client Callback:
Server Usage:

Best Practices

Use Descriptive Names: Name your callbacks clearly to indicate what they do
  • Good: 'rsg-shop:server:purchaseItem'
  • Bad: 'shop:buy'
Always Validate Data: Never trust client-sent data without validation
Always Provide Fallbacks: Handle cases where data might be nil

Performance Tips

  1. Donโ€™t spam callbacks: Avoid calling callbacks in loops or every frame
  2. Cache results: If data doesnโ€™t change often, cache it instead of calling repeatedly
  3. Use events for one-way communication: If you donโ€™t need a response, use events instead

Common Patterns

Pattern 1: Permission Check

Pattern 2: Database Fetch

Pattern 3: Multi-Step Validation


Troubleshooting

Callback Not Responding

Ensure CreateCallback is called before you try to trigger it:
Look in your server/client console for Lua errors that might prevent the callback from executing
Make sure the resource containing the callback is actually started:

Callback Returns Nil

Common causes:
  • Player object is nil (player left server)
  • Database query returned no results
  • Logic error in callback function
Always handle nil returns:

Advanced: Async/Await Pattern

For cleaner code, you can wrap callbacks in promises:

Summary

  • Server Callbacks: Client requests data from server
    • CreateCallback on server
    • TriggerCallback on client
  • Client Callbacks: Server requests data from client
    • CreateClientCallback on client
    • TriggerClientCallback on server
  • Always validate data on the server
  • Always handle nil responses
  • Use events for one-way communication
  • Cache data when possible to avoid repeated callbacks

Next Steps


Need help? Join the RSG Framework Discord!