My DataStore isn't working

I’ve been working on this forever and even though I’ve done everything right. My dataStore still wont work. The goal is that everytime you drink a bottle of water your “Drinks” count goes up by one.

Sometimes it will save the Data if I change it manually, but if I use a tool that changes it, it never saves

I’ve looked all over the forums and tried multiple solutions and non of them worked. Can someone please help me

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local HydrationFolder = Instance.new("Folder")
	HydrationFolder.Name = "HydrationFolder"
	HydrationFolder.Parent = player
	
	local Drinks = Instance.new("IntValue")
	Drinks.Name = "Drinks"
	Drinks.Parent = leaderstats
	
	local Ammo = Instance.new("IntValue")
	Ammo.Name = "Ammo"
	Ammo.Parent = player
	
	local MaxHydration = Instance.new("IntValue")
	MaxHydration.Name = "MaxHydration"
	MaxHydration.Parent = HydrationFolder
	MaxHydration.Value = 100
	
	local pUserId = ""..player.UserId
	local dData
	local success, errormessage = pcall(function()
		dData = myDataStore:GetAsync(pUserId)
	end)

	if success then
		if dData ~= nil then
			Drinks.Value = dData
		end
		print("Player data successfully loaded.")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local pUserId = ""..player.UserId
	
	local dData = player.leaderstats.Drinks.Value
	local success, errormessage = pcall(function()
		myDataStore:UpdateAsync(pUserId,function(oldValue)
			if oldValue ~= nil then
				print("oldValue: "..oldValue)
			end
			print("data: "..dData)
			return dData
		end)
	end)
	if success then
		print("Player Data successfully saved.")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	if not RunService:IsStudio() then
		local players = game.Players:GetPlayers()
		for _, player in pairs(players) do
			player:Kick("Server is shutting down: Please rejoin, your data will be saved.")	
		end
		while true do
			wait()
		end
	else
		wait(2)
	end
end)

Where is data? I don’t see it anywhere as a variable, only seeing it set equal.

I swapped my code to the one up there and it still isnt working

Where’s your drinking code? If you’re changing the value on the client it won’t replicate to the server

You could use GlobalDataStore:IncrementAsync(key, value) to increase the drinks count.

Retrieving and updating the datastore could slow down the server’s performance or may encounter an error if dealt multiple times.

Switch to ProfileService, way easier.

I found out why, I needed to use a remote event for my tool.