Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I save a players leaderstats? [closed]

Asked by 5 years ago

I want to save players' money but any scripts I find do not work

0
datastore User#23365 30 — 5y
0
^ greatneil80 2647 — 5y

Closed as Not Constructive by valchip, green271, Vulkarin, and User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
2
Answered by
SmartNode 383 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
Good Pseudo Code! hipenguinflip 30 — 5y
Ad