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
UseRSGCore.Functions.CreateCallback on the server to create a callback:
Parameters:
source: The player who triggered the callbackcb: The callback function to send data back...: Any additional arguments passed from the client
Triggering a Server Callback
UseRSGCore.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
UseRSGCore.Functions.CreateClientCallback on the client to create a callback:
Triggering a Client Callback
UseRSGCore.Functions.TriggerClientCallback on the server to call the callback:
Practical Examples
Example 1: Checking if Player Has Item
Server Callback:Example 2: Getting Playerโs Horses
Server Callback with Database:Example 3: Purchasing Item
Server Callback:Example 4: Client Callback for Coordinates
Client Callback:Best Practices
Always Provide Fallbacks: Handle cases where data might be nil
Performance Tips
- Donโt spam callbacks: Avoid calling callbacks in loops or every frame
- Cache results: If data doesnโt change often, cache it instead of calling repeatedly
- 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
Check callback is registered
Check callback is registered
Ensure
CreateCallback is called before you try to trigger it:Check for errors
Check for errors
Look in your server/client console for Lua errors that might prevent the callback from executing
Verify resource is started
Verify resource is started
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
Advanced: Async/Await Pattern
For cleaner code, you can wrap callbacks in promises:Summary
-
Server Callbacks: Client requests data from server
CreateCallbackon serverTriggerCallbackon client
-
Client Callbacks: Server requests data from client
CreateClientCallbackon clientTriggerClientCallbackon 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
- Server Events Guide - One-way server-client communication
- Player Data - Access and modify player data
- Server Functions Reference - Complete function list
- Inventory Functions - Work with inventory items
Need help? Join the RSG Framework Discord!