Stat Saving System

Hello, I am wondering if there is a simple way I could add a quick stat saving system to this script:
Statsaving
I am really not that great at scripting and that is why I am asking for some help. Thanks!

3 Likes

you’ll need to add all the values in one dictionary, and save that for every player on Players.PlayerRemoving.

 local DataStoreService = game:GetService("DataStoreService")
 local DataStore = DataStoreService:GetDataStore("Stats", "scope1")
  
 Players.PlayerRemoving:Connect(function(player)
 local stats = {}
 
 for _, dataType in ipairs(player.leaderstats:GetChildren()) do
        stats[dataType.Name] = dataType.Value
 end
 
 DataStore:SetAsync(player.UserId, stats)

 end)

when a player joins, retrieve values like this :

 --> let's assume the values saved were called Coins, Cash and Thing
 local stats = DataStore:GetAsync(player.UserId)
 local Coins, Cash, Thing = stats.Coins, stats.Cash, stats.Thing

remember to use protected calls for requests

1 Like

So can this be in the same script? Or does it have to be in a different one. I don’t mind it just clears things up for me.

it can be in any, though it is preferable to do it in the same so you don’t have to index ValueBase objects twice, and just change the value for each type as it is retrived

There is an error saying this: ServerScriptService.leaderstats:15: attempt to index nil with ‘playerRemoving’.
Why is that?

because I only gave a demonstration, that is not the complete working code to be pasted; it only has a few stuff left out for yourself.

Players is not a global, do this intead

game:GetService("Players").PlayerAdded:Connect(function(p)
-- instead of your own version
-- and don't use two PlayerAdded events

you will rarely be given working code here, the category’s just for helping you with code, not to structure it entirely

I pretty much said I have no idea what I am doing but fair enough.