Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I save a players leaderstats?

1 answer

To save a players' leaderstats (aka "money" in your case) you should research about DataStore.


Method 1: Basic

When Player Enters,

  • If they have data stored already,

    • Get the data,

      • Replace the current values with the saved values.
  • If they don't have any data,

    • Create a new data file,

      • Replace the data file with a default value,

        • And replace the current values with the default values.

When Player Leaves,

  • Save their data

Overall, if you're trying to save a player's stats without having to research all of this stuff, use this code:

--[ Variables ]--
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("7D7FD5D49A1AD4B650017503FBDF93C5")

--[ Player Enter: Load Data ]--
game.Players.PlayerAdded:Connect(function(player)

 --[ Create Leaderstats ]--
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstats"

 local Cash = Instance.new("IntValue",leader)
 Cash.Name = "Money"

 --[ Load Values ]--
 Cash.Value = ds:GetAsync(player.UserId) or 0

 --[ Prevent Data Loss: 1 ]--
 ds:SetAsync(player.UserId, Cash.Value)

 --[ Prevent Data Loss: 2 ]--
 Cash.Changed:connect(function()

  ds:SetAsync(player.UserId, Cash.Value)

 end)

end)

--[ Player Leave: Save Data ]--
game.Players.PlayerRemoving:Connect(function(player)

 --[ Save Data ]--
 ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)

end)

I was able to remove the deprecated code so that you won't get called out for.

1 comment

0

Good Pseudo Code!

This discussion is preserved as it was. New answers, comments, and votes are closed.