local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.leaderstats.Points.Value) 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
local PlayerPoints = ds:GetAsync(player.UserId) if PlayerPoints ~= nil then Points.Value = PlayerPoints 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
local GetSaved = DS:GetAsync(plrkey) if GetSaved then savevalue.Value = GetSaved[1] else local NumbersForSaving = {savevalue.Value} DS:GetAsync(plrkey, NumbersForSaving) end
end)
game.Players.PlayerRemoving:Connect(function(plr) DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Points.Value}) end)
BadGold = script.Parent isHidden = false --Give the player a Points leaderboard when they join the game game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Points = Instance.new("IntValue") Points.Parent = leaderstats Points.Name = "Points" Points.Value = 0 end) --Fires a function when the player touches the BadGold BadGold.Touched:Connect(function(hit) --if the BadGold is not hidden and the thing touching the BadGold is a player... if(not isHidden and game.Players:FindFirstChild(hit.Parent.Name))then --give the player one point and make the brick hidden again local player = game.Players[hit.Parent.Name] player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 BadGold.Transparency = 1 isHidden = true wait(2) -- wait 2 seconds to make the BadGold visible again after being touched isHidden = false BadGold.Transparency = 0 end end)
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.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(player) pcall(function() local player.leaderstats.Points.Value = ds:GetAsync(player.UserId, player.leaderstats.Points.Value) or 0 end end game.Players.PlayerRemoving:connect(function(player) pcall(function() ds:SetAsync(player.UserId, player.leaderstats.Points.Value) end 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!