Oops, I forgot to parent the leaderstats in that example code. insert it in between here:
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr -- insert it in between these other lines of code
local cash = Instance.new("IntValue")
Here is the fixed code:
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")
local defaultCash = 50 -- change amount to your liking
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, message = pcall(function()
data = dataStore:GetAsync(plr.UserId.."-cash")
end
if success then
if data then
cash.Value = data
else
print(plr.Name.." is a new player")
cash.Value = defaultCash
end
else
warn(message)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
local data = plr.leaderstats.Cash.Value
local success, message = pcall(function()
dataStore:SetAsync(plr.UserId.."-cash", data)
end
if success then
print("data for "..plr.Name.." saved")
else
warn(message)
end
end