Why my script for DataStore doesn't work?

I’m going to debug it in studio give me a little bit I’m going to play around with it.

Were is this script located is it in workspace, Server script service, etc

You’re not loading your data. Your script is always writing 0 to the data store because you set cash.Value to zero during initialization.

What I should do? Sorry i’m really stupid at data stores

Where is my script I set cash.Value to zero?

In your players.PlayerAdded function, you ran
cash.Value = 0

It might not save properly if you run the code in Studio. Try running it in the roblox application.

You should use GetAsync to retrieve the value of cash from the datastore

A simple datastore script:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("Data")

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 = 0 --default value if no data
   cash.Parent = leaderstats

   local data
   local success, errormessage = pcall(function()
      data = ds:GetAsync("Player_"..player.UserId)
   end)

   if not success then
      warn("Not able to get "..player.Name.."'s data! Error message: "..errormessage
   else
      cash.Value = data.Cash
   end
end)

game.Players.PlayerRemoving:Connect(function(player)
   local data = {
   Cash = player.leaderstats.Cash.Value
   }
   local success, errormessage = pcall(function()
      ds:SetAsync("Player_"..player.UserId, data)
   end

   if not success then
      warn("Not able to save "..player.Name.."'s data! Error message: "..errormessage
   end
end)

:BindToClose is not needed

AND, this script does not restrict saving only Cash, but instead a table of the data containing the values

Are you told about script that I provide in the description?

doesn’t work :confused: you have many errors in your code

like? I reviewed it and no errors?

no, no any error. Does it work for you?

can I see the output?

It’s been a while since i’ve done basic datastore bc i use profileservice now but from what I see, this should work

My output is clear, I added some logs but the print that all should be work!