I'm trying to make my game save the cash players have and came up with this
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("error")
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("Data Saved")
else print("error")
warn("errormessage")
end
end)
Roblox says there is nothing that needs to be corrected --ex. unknown value-- and nothing is printed (the errors)

2 comments
Oh and make sure StudioAccessToAPIServices are enabled!
No but the print is on the player removing not on the player added