I am able to get my player Cash value to load. When I spend money in game the leaderstats change how they should. When I leave and re-join, my player's Cash value is back to the starting_cash value. I am also getting an error saying "Argument 2 missing or nil". Can someone give any idea how to make the cash value actually save?
local Players = game:GetService("Players") local CashDataStore = game:GetService("DataStoreService"):GetDataStore("Cash") local STARTING_CASH = 100 local function onPlayerAdded(player) local playerKey = "Player_" .. player.UserId print("User ".. player.UserId.. " Joined") local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" local Cash = Instance.new("IntValue", leaderstats) Cash.Name = "Cash" local myCash local success, err = pcall(function() myCash = CashDataStore:GetAsync(playerKey) or STARTING_CASH end) if success then Cash.Value = myCash print("loading "..myCash) else -- Failed to retrieve data end leaderstats.Parent = player end for _, player in pairs(Players:GetPlayers()) do onPlayerAdded(player) end Players.PlayerAdded:Connect(onPlayerAdded) --------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------- local function onPlayerLeft(player) local playerKey = "Player_"..player.UserId local savedCash = player.leaderstats.Cash.Value savedCash = CashDataStore:UpdateAsync(playerKey) print("User "..player.UserId.." disconnected") print("User has "..savedCash.." in the bank") end Players.PlayerRemoving:Connect(onPlayerLeft)
You are not updating the value in UpdateAsync. It's supposed to have 2 parameter, key and value you want to update.
replace line 44 to:
savedCash = CashDataStore:UpdateAsync(playerKey, savedCash)