local ds = game:GetService(“DataStoreService”):GetDataStore(“SaveData”)
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = “id_”…plr.userId
local save1 = plr.leaderstats.Cash
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
else
local NumberForSaving = {save1.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)
Firstly, always use a pcall() when accessing the data store. It is a network request, so it can throw errors. Secondly, try not using UpdateAsync(). Thirdly, I can’t seem to find where you have created that Money value in the player’s leaderstats.
--my script
local function load(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue", leaderstats)
money.Name = "Money"
local playerData
local success, err = pcall(function()
playerData = dataStore:GetAsync("id_"..player.UserId)
end)
if success and playerData then
money.Value = playerData["Money"]
elseif not success and err then
load(player)
end
end
local function save(player)
local saveData = {["Money"] = player.leaderstats.Money.Value}
local success, err = pcall(function()
dataStore:SetAsync("id_"..player.UserId, saveData)
end)
if success then
print("Saved!")
elseif not success and err then
save(player)
end
end
game.Players.PlayerAdded:Connect(load)
game.Players.PlayerRemoving:Connect(save)
Please note I have not included default data for new players here.