1 | local DataStore = game:GetService( "DataStoreService" ) |
2 | local ds = DataStore:GetDataStore( "CashSaveSystem" ) |
3 |
4 | game.Players.PlayerRemoving:connect( function (player) |
5 | ds:SetAsync(player.UserId, player.leaderstats.Points.Value) |
6 | end ) |
that is the code i need to data store but i cant find any way to, i cant really make a new script because i have a game based on this script and making a new one would end up just destroying my game
AS you want to get the stats back after saving you would be using the GetAsync part of datastores. As its just points the script is easy.
Add this bit of code after your Points.Value = 0
1 | local PlayerPoints = ds:GetAsync(player.UserId) |
2 |
3 | if PlayerPoints ~ = nil then |
4 | Points.Value = PlayerPoints |
5 | end |
Its that simple
So first of all you need to turn on studio api services. So instead of using that script, I would recommend using this. So insert a script in ServerScriptService and name it whatever you want, copy and paste this code in it. Every time you add a new stat then, basically make another line under savevalue = plr.leaderstats.Points and type in something like savevalue2 = plr.leaderstats.Cash and dont forget to do savevalue2.Value and at very bottom put a comma after the leaderstats.Points.Value and add plr.leaderstats.Cash.Value. Hope this was helpful.
local DS = game:GetService("DataStoreService"):GetDataStore("Datae") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey = "id_"..plr.userId local savevalue = plr.leaderstats.Points
1 | local GetSaved = DS:GetAsync(plrkey) |
2 | if GetSaved then |
3 | savevalue.Value = GetSaved [ 1 ] |
4 |
5 | else |
6 | local NumbersForSaving = { savevalue.Value } |
7 | DS:GetAsync(plrkey, NumbersForSaving) |
8 | end |
end)
game.Players.PlayerRemoving:Connect(function(plr) DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Points.Value}) end)
01 | BadGold = script.Parent |
02 | isHidden = false |
03 |
04 | --Give the player a Points leaderboard when they join the game |
05 | game.Players.PlayerAdded:Connect( function (player) |
06 | local leaderstats = Instance.new( "Folder" ) |
07 | leaderstats.Name = "leaderstats" |
08 | leaderstats.Parent = player |
09 |
10 | local Points = Instance.new( "IntValue" ) |
11 | Points.Parent = leaderstats |
12 | Points.Name = "Points" |
13 | Points.Value = 0 |
14 | end ) |
15 |
the rest of the code is for when i touch a part i get a point. i thought it will help you to know
you set the data. but don't load it in when the player joins the game. and i recommend to put it in a pcall() so it won't stop the whole script if it errors.
01 | local DataStore = game:GetService( "DataStoreService" ) |
02 | local ds = DataStore:GetDataStore( "CashSaveSystem" ) |
03 |
04 | game.Players.PlayerAdded:connect( function (player) |
05 | pcall ( function () |
06 | local player.leaderstats.Points.Value = ds:GetAsync(player.UserId, player.leaderstats.Points.Value) or 0 |
07 | end |
08 | end |
09 |
10 | game.Players.PlayerRemoving:connect( function (player) |
11 | pcall ( function () |
12 | ds:SetAsync(player.UserId, player.leaderstats.Points.Value) |
13 | end |
14 | end ) |
the reason why i put "or 0" is because if the player joins and they don't have any data then we would need to change it to 0. hope this helped!