Data Store Not Working

So i was creating a Leader stats Data Store System but it seemed to not be working, I’ve been testing new things and it seems there are no errors but it still will not work. Here is the script:

local dataStoreService = game:GetService("DataStoreService")
local cashData = dataStoreService:GetDataStore("cashData")

game.Players.PlayerAdded:Connect(function(player)
		local leaderstats =  Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	
		local cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Value = cashData:GetAsync(player.UserId) or 0
		cash.Parent = leaderstats
	
		cash.Changed:Connect(function()
			cashData:SetAsync(player.UserId, cash.Value)
		end)
end)

If anyone has an answer to my problem please reply to the post! Thank you!

I would suggest not having it on .Changed since datastore functions have a cooldown, save the value when the player leaves with .PlayerRemoving

Also if the cash never changes it won’t save so its kinda useless.

Just an example of how to save it

game:GetService('Players').PlayerRemoving:Connect(function(player)
  local leaderstats = player:FindFirstChild('leaderstats')
  if leaderstats then
    local cash = leaderstats:WaitForChild('Cash')
    if cash then
      cashData:SetAsync(player.UserId, cash.Value)
    end
  end
end)

Also how are you editting the amount of cash a player has?

2 Likes