local players = game:GetService("Players") -- Gets the player service
local DSS = game:GetService("DataStoreService") -- Gets the datastore service
local datastore = DSS:GetDataStore("sugarSimulatorDataStore") -- Gets the game's datastore
players.PlayerAdded:connect(function(player) -- parameter for localplayer
local folder = Instance.new("Folder") - Creates leaderstats
folder.Name = "leaderstats"
folder.Parent = player
local currency1 = Instance.new("IntValue")
currency1.Name = "Sugar"
currency1.Parent = folder
local currency2 = Instance.new("IntValue")
currency2.Name = "Rebirths"
currency2.Parent = folder
local save = nil -- defines the save varaible
local success, errormessage = pcall(function() -- pcall
local save = datastore:GetAsync(player.UserId) - gets the save
end)
if save.cash then -- see if there if the sugar is there
player.leaderstats.Sugar.Value = save.cash -- gives it
end
if save.rebirths then
player.leaderstats.Rebirths.Value = save.rebirths
end
end)
players.PlayerRemoving:Connect(function(player) - another localplayer parameter
if player.leaderstats.Sugar then --if a leaderstats
local sugar = player.leaderstats.Sugar.Value -- sets sugar to sugar leaderstat value
end
if player.leaderstats.Rebirths then
local rebirths = player.leaderstats.Rebirths.Value
end
local stuff = { -- table of the save items
["cash"] = player.leaderstats.Sugar.Value;
["rebirths"] = player.leaderstats.Rebirths.Value;
}
datastore:SetAsync(player.UserId, stuff) -- saves
end)
If you see anything please tell me! Thanks!

5 comments
What error / problems are you experiencing?
ServerScriptService.SugarRebirthsLeaderstatsSave:24: attempt to index nil with 'cash'
It could be because it is a new datastore, the player might not have any stats given and since you are using save = nil, the script can't find anything in a nil. You also do not need to redefined save in when getting the save from datastore since you already defined it previously. I explained very badly but hope you understand.. haha
I played the game 7 times and it did not save
No errors? If so, it might be because the server did not have enough time to run the code before the game shuts down.