Skip to main content

Introduction

The Player Data system is the heart of RSG Frameworkโ€™s player management. It handles all player information including character data, jobs, gangs, money, metadata, and inventory. Understanding this system is essential for creating resources that interact with players. Location: resources/[framework]/rsg-core/server/player.lua
Player data is automatically saved to the database at intervals defined in config (UpdateInterval), and on logout.

Core Features

๐Ÿ“‹ Comprehensive Data Management

  • Character Info: Names, birthdate, gender, nationality
  • Money System: Multiple currency types with transaction logging
  • Jobs & Gangs: Full hierarchy system with grades and boss status
  • Metadata: Health, hunger, thirst, stress, and custom data
  • Reputation: Flexible reputation system for any activity

๐Ÿ”„ Real-time Synchronization

  • StateBags: Instant sync for frequently updated data
  • Events: Server and client events for data changes
  • Auto-save: Periodic database persistence

โš™๏ธ Flexible API

  • Player Functions: Comprehensive function set for data manipulation
  • Direct Access: Read player data directly from PlayerData table
  • Extension: Add custom methods and fields dynamically

Core Server Functions

RSGCore.Player.Login

  • On player login, gets their data or sets default values

RSGCore.Player.CheckPlayerData

  • Function called above on player join to gather player data (this is where you can add/remove additional player data)

RSGCore.Player.Logout

  • Saves player on logout and removes them from active players table

RSGCore.Player.CreatePlayer

  • Creates a new character and sets default data (any function inside the self table can be called on the player after using the GetPlayer function)

RSGCore.Player.Save

  • Saves the playerโ€™s info to the database

RSGCore.Player.DeleteCharacter

  • Deletes a character and all corresponding data in the database

Player Object Functions

Once you have a Player object (via RSGCore.Functions.GetPlayer(source)), you can use the following functions on that player:

Player.Functions.SetJob

  • Sets a playerโ€™s job and grade

Player.Functions.SetGang

  • Sets a playerโ€™s gang and grade

Player.Functions.SetJobDuty

  • Sets a playerโ€™s on/off duty status for their job

Player.Functions.AddMoney

  • Add money to a playerโ€™s account

Player.Functions.RemoveMoney

  • Remove money from a playerโ€™s account

Player.Functions.SetMoney

  • Set a playerโ€™s money to a specific amount

Player.Functions.GetMoney

  • Get the amount of money a player has in a specific account

Player.Functions.SetMetaData

  • Set metadata for a player

Player.Functions.GetMetaData

  • Get metadata for a player

Player.Functions.AddRep

  • Add reputation points for a specific reputation type
Reputation System: The framework includes a flexible reputation system stored in metadata.rep. You can create any reputation types you need (hunting, fishing, crafting, lawman, outlaw, etc.)

Player.Functions.RemoveRep

  • Remove reputation points for a specific reputation type

Player.Functions.GetRep

  • Get reputation points for a specific reputation type
Common Reputation Types:
  • hunting - Tracking, animal kills, perfect pelts
  • fishing - Fish caught, legendary fish
  • crafting - Items crafted, quality of work
  • lawman - Criminal arrests, bounties collected
  • outlaw - Crimes committed, bounties escaped
  • trader - Goods sold, trading reputation
  • doctor - Patients healed, medical services
  • Custom types as needed for your server
Reputation Use Cases:

Player.Functions.HasItem

  • Check if player has a specific item

Player.Functions.SetPlayerData

  • Set any player data field

Player.Functions.Save

  • Manually save a playerโ€™s data to database

Player.Functions.Logout

  • Log a player out (removes from active players)

Player.Functions.UpdatePlayerData

  • Trigger an update of player data to client

Accessing Player Data

Once you have a Player object, you can access their data directly:

StateBags System

The RSG Framework uses FiveMโ€™s StateBags system to sync specific metadata between server and client in real-time. This provides better performance than constantly triggering events.

What are StateBags?

StateBags are key-value stores attached to entities (players, vehicles, etc.) that automatically sync between server and client. Theyโ€™re more efficient than events for frequently updated data. Synced Metadata Keys:
  • hunger (0-100)
  • thirst (0-100)
  • cleanliness (0-100)
  • stress (0-100)
  • health (0-600)
  • isLoggedIn (boolean)

How StateBags Work

Reading StateBags (Client-Side)

Using StateBags in Resources

Benefits of StateBags

Performance: No need to constantly trigger events for metadata updates
Real-time: Changes sync instantly without event delays
Clean Code: Access data directly instead of callback hell

When to Use StateBags vs Events

Use StateBags for:
  • Frequently updated data (hunger, thirst, health)
  • Data that needs instant client access
  • Simple value syncing
Use Events for:
  • Complex data updates
  • Actions that need validation
  • One-time notifications

Example: Custom HUD Using StateBags


Complete Metadata Structure

The full metadata table structure in PlayerData:

Best Practices

Always Check Player Exists: Before accessing player data, always verify the player object exists to prevent errors
Donโ€™t Modify PlayerData Directly: Always use Player.Functions to modify data - this ensures events fire and data syncs properly
Use StateBags for HUDs: For frequently updated display data (hunger, thirst, health), read from StateBags instead of triggering events

Performance Tips

  1. Batch Updates: When setting multiple metadata values, group them to reduce database writes
  2. Cache Player Objects: Store the Player object in a variable instead of calling GetPlayer multiple times
  3. Use GetRep Sparingly: If checking reputation frequently, cache the value locally
  4. Avoid Save Spam: Donโ€™t call Player.Functions.Save() excessively - let auto-save handle it

Common Patterns


Next Steps


Need help? Join the RSG Framework Discord!