I change the value of Coins for ex. and it doesn't save next time I test the game
local DS = game:GetService("DataStoreService"):GetDataStore("ScriptingTestXOXOXO19191")--DO NOT CHANGE UNLESS YOU WANT EVERYONES STATS TO BECOME 0 AGAIN! game.Players.PlayerAdded:Connect(function(plr) local stats = Instance.new("Folder",plr) stats.Name = "leaderstats" local Coins = Instance.new("IntValue",stats) Coins.Name = "Coins" local Donuts = Instance.new("IntValue",stats) Donuts.Name = "Donuts" local Rebirths = Instance.new("IntValue",stats) Rebirths.Name = "Rebirths" local key = "player-"..plr.userId local savedValues = DS:GetAsync(key) if savedValues then Coins.Value = savedValues[1] Donuts.Value = savedValues[2] Rebirths.Value = savedValues[3] else local valueToSave = {Coins.Value, Donuts.Value, Rebirths.Value} DS:SetAsync(key, valueToSave) end end) --Saving game.Players.PlayerRemoving:Connect(function(plr) local key = "player-"..plr.userId local stat = plr.leaderstats local valueToSave = {stat.Coins.Value, stat.Donuts.Value, stat.Rebirths.Value} local success, err = pcall(function() DS:SetAsync(key, valueToSave) end) if success then print("Saving was successful") else error("Saving was not successful") end end)
Just use this Script in and put it into ServerScriptService!
(Turn Studio API Services on!)
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local cash = Instance.new("IntValue") cash.Name = "Cash" cash.Parent = leaderstats local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId.."-cash") end) if success then cash.Value = data else print("There was an error whilst getting your data") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value) end) if success then print("Player Data successfully saved!") else print("There was an error when saving data") warn(errormessage) end end)