Overview: This guide shows you how to use the LeaderstatModule
to manage player stats in your Roblox game. This module makes it easy to create and manage stats, save them at regular intervals, and save data when players leave. You can name your stats anything you like.
Link to LeaderstatModule Script
How to Use LeaderstatModule
- Add the Module to Your Game
- In
ServerScriptService
, create a new Script. - Use the
LeaderstatModule
to handle player stats.
- Add Stats for New Players
- Use
LeaderstatModule.AddStat(player, "StatName", initialValue)
to create stats. - Replace
"StatName"
with the name of your stat (like"Cash"
or"Score"
). - Replace
initialValue
with the starting value (like0
).Example:
lua
Copy code
LeaderstatModule.AddStat(player, "Cash", 0)
- Set Up Auto-Saving
- Decide how often you want to auto-save stats (in seconds).
- Use
LeaderstatModule.SetupAutoSave(player, interval)
to start auto-saving. - Replace
interval
with the number of seconds between auto-saves.Example:
lua
Copy code
task.spawn(LeaderstatModule.SetupAutoSave, player, 300) -- Saves every 5 minutes
- Save Stats When a Player Leaves
- Use
LeaderstatModule.SaveAllStats(player)
to save stats when a player leaves the game. - Make sure this function runs when the player is removed.
- Customize Your Stats
- Name your stats whatever you want by changing the
AddStat
parameters. - For example, create a
"Level"
stat with an initial value of1
.Example:
lua
Copy code
LeaderstatModule.AddStat(player, "Level", 1)
Example Script
Here’s a simple script to get you started:
lua
Copy code
local LeaderstatModule = require(game.ServerScriptService.LeaderstatModule)
game.Players.PlayerAdded:Connect(function(player)
-- Set up stats for the player
LeaderstatModule.AddStat(player, "Cash", 0)
LeaderstatModule.AddStat(player, "Level", 1)
-- Set up auto-saving every 300 seconds
task.spawn(LeaderstatModule.SetupAutoSave, player, 300)
end)
game.Players.PlayerRemoving:Connect(function(player)
-- Save all stats when the player leaves
LeaderstatModule.SaveAllStats(player)
end)
Conclusion
Follow these steps to manage player stats easily with the LeaderstatModule
. Customize the stats as needed and make sure your data is saved. If you have any questions, feel free to ask!