Cash Problem saving and showing

I want it so it shows player’s cash on the leaderboard and saves their cash, so they can use it to buy in-game items. I want a GUI that shows how much cash they have.
it might be an error in the script or a setting am very new IDK why it’s broken. It doesn’t give any errors.

local DSS = game:GetService("DataStoreService")
local Money = DSS:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(plr)
	local pCash = Money:GetAsync(plr.UserId) or 0
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "Leaderstats"
	local CashValue = Instance.new("NumberValue")
	CashValue.Name = "Cash"
	CashValue.Value = pCash
	CashValue.Parent = leaderstats
	CashValue.Changed:Connect(function(v)
		Money:SetAsync(plr.UserId, v)
		
	end)
end)

1 Like

That needs to be all lower case, otherwise the CoreGui won’t pick it up:

leaderstats.Name = "leaderstats"   -- lowercase "L" to start

Changed first parameter is the property that changed, not the value. Btw I don’t recommend using this event, instead use PlayerRemoving and BindToClose()

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local Money = DSS:GetDataStore("Cash")

Players.PlayerAdded:Connect(function(client)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = client
	
	local CashValue = Instance.new("NumberValue")
	CashValue.Name = "Cash"
	
	local Data
	
	local Success, Error = pcall(function()
		Data = Money:GetAsync(client.UserId) or 0
	end)
	
	if Success then
		CashValue.Value = Data
	end
	CashValue.Parent = leaderstats
end)

Players.PlayerRemoving:Connect(function(client)
	pcall(function()
		Money:SetAsync(client.UserId, client.Cash.Value)
	end)
end)

game:BindToClose(function()
	for i, client in pairs(Players:GetPlayers()) do
		pcall(function()
			Money:SetAsync(client.UserId, client.Cash.Value)
		end)
	end
end)
1 Like

Thanks, the leader board works but it still doesn’t save still no errors. Could be a setting?

Sorry, I forgot to add leaderstats. Try this:

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local Money = DSS:GetDataStore("Cash")

Players.PlayerAdded:Connect(function(client)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = client

	local CashValue = Instance.new("NumberValue")
	CashValue.Name = "Cash"

	local Data

	local Success, Error = pcall(function()
		Data = Money:GetAsync(client.UserId) or 0
	end)

	if Success then
		CashValue.Value = Data
	end
	CashValue.Parent = leaderstats
end)

Players.PlayerRemoving:Connect(function(client)
	pcall(function()
		Money:SetAsync(client.UserId, client.leaderstats.Cash.Value)
	end)
end)

game:BindToClose(function()
	for i, client in pairs(Players:GetPlayers()) do
		pcall(function()
			Money:SetAsync(client.UserId, client.leaderstats.Cash.Value)
		end)
	end
end)
1 Like

Thank you for helping me. It all seems to work now.