Cash script isn't saving to DataStore properly

The title pretty much some it up, my leaderstats cash script, isn’t saving to the Data Store. Please tell me why. Thanks Devs.

local DataStoreService = game:GetService("DataStoreService")
local PlayerCash = DataStoreService:GetDataStore("PlayerCash")

function OnPlayerAdded(player)
	local stats = Instance.new("Folder", player)
	stats.Name = 'leaderstats'
	local Cash = Instance.new("NumberValue", stats)
	Cash.Name = "Cash"
	Cash.Value = 0
	local data
	local success, err = pcall(function()
		data = PlayerCash:GetAsync(player.UserId)
	end)
	if success then
		print("Data loaded!")
	else
		print("There was an error While Getting Data" ..player.Name)
		warn(err)
	end

end

function OnPlayerRemoving(player)
	local success, err = pcall(function()
		PlayerCash:SetAsync(player.UserId, player.leaderstats:FindFirstChild("Cash").Value)
	end)
	if success then
		print("Data Saved!")
	else
		print("There was an error while saving data of player " .. player.Name)
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)
game.Players.PlayerRemoving:Connect(OnPlayerRemoving)

Moved to Scripting Support. Be more careful in the future, read the pinned About topics under each category.

1 Like

You’re just retrieving data here, not saving.
Show us the part where you save data to the DataStore

Ohh sorry, give me a sec
30 chaahrhaihaidhaid

OKAY ITS STILL NOT SAVING PLEASE HELP! For some reason the data is still not saving

it saves, you’re just not setting the ValueObject’s value to the retrieved data

After you did local data = GetAsync(etc.) , just set the Cash object’s (through the variable) Value property to data

	local success, err = pcall(function()
		data = PlayerCash:GetAsync(player.UserId)
        Cash.Value = data or 0 -- default
	end)
3 Likes