How do I make this leaderstats script save to the Datastore?
I have a script in one of my games that handles money being given out every 5 minutes, and
I need it to save to the datastore so when the player rejoins the game after buying/earning the money they keep it, instead of their money counter resetting.
The code is here to help you understand what I am talking about:
01 | game.Players.PlayerAdded:Connect( function (Plr) |
02 | local Stats = Instance.new( "Folder" , Plr) |
03 | Stats.Name = "leaderstats" |
05 | local Cash = Instance.new( "IntValue" , Stats) |
09 | local DS = game:GetService( "DataStoreService" ) |
11 | game.Players.PlayerRemoving:Connect( function () |
12 | DS:SetAsync(Plr.userId.. "_DS" , Cash.Value) |
17 | for i, v in pairs (game.Players:GetPlayers()) do |
18 | v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + 100 |
As you can see in the script above I tried getting an understanding of the Datastore service
1 | local DS = game:GetService( "DataStoreService" ) |
3 | game.Players.PlayerRemoving:Connect( function () |
4 | DS:SetAsync(Plr.userId.. "_DS" , Cash.Value) |
But, that did not work. Could I get some help on this?