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

How Can You Make A Leaderstat Saving System?

Asked by 4 years ago

I've been trying to study on how this datastore system works, I'm still a bit stuck on this saving script. This is what I got.

01local DataStore = game:GetService("DataStoreService")
02 
03 
04game.Players.PlayerAdded:connect(function(player)
05 local leader = Instance.new("Folder",player)
06 leader.Name = "leaderstats"
07 local Cash = Instance.new("IntValue",leader)
08 Cash.Name = "Cash"
09 Cash.Value = ds:GetAsync(player.UserId) or 0
10 ds:SetAsync(player.UserId, Cash.Value)
11 Cash.Changed:connect(function()
12  ds:SetAsync(player.UserId, Cash.Value)
13end)
14 
15 
16game.Players.PlayerRemoving:connect(function(player)
17 ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
18end)

It would really be helpful if you could help me :D

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago
01local DataStore = game:GetService("DataStoreService")
02local ds = DataStore:GetDataStore("MyDataStore")
03 
04game.Players.PlayerAdded:connect(function(player)
05 local leader = Instance.new("Folder",player)
06 leader.Name = "leaderstats"
07 local Cash = Instance.new("IntValue",leader)
08 Cash.Name = "Cash"
09 
10local data
11 
12local success, errormessage pcall(function()
13 
14data = ds:GetAsync(player.UserId.."-cash")
15    Cash.Value = data
View all 23 lines...

Note: Wrapped It In Pcalls And Made It A Bit Better (This Is The Way I Learned)

Learn More Here: https://developer.roblox.com/en-us/articles/Data-store

0
Thank you, I think Pcalls make it so if the function breaks it won't break the entire script, very helpful! CodedStars 70 — 4y
Ad

Answer this question