Issue with adding values

Hi there! I have found a strange issue with adding numbers. The first number I add to a stat takes away the default value, then it adds as it should. As far as I can tell, it goes back to the default value not showing in the data store script.

Stats Script

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	
	local tableToSave = {
		player:FindFirstChild("Cash").Value
	}
	
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	local cash = Instance.new("IntValue", player)
	cash.Name = "Cash"
	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
		
	end)
	
	if success then -- If there were no errors and player loaded the data
		
		cash.Value = data[1]
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!")
		cash.Value = 100 --Default value on first join. Change to whatever
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)
	
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)
		
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

So, I have a script that makes it 100 in my cash displayed, that basically just checks if the value is 0, then adds 100. This makes it 100, but throws peculiar errors. Adding to it removes the default value. This is the same for dev product scripts, and adding stats. They all use +, but remove the value. Say if I was adding 10 every 5 seconds, it would make it 10 instead of 100, but then would continue to add 10. This is the add stats script:

local player = game.Players
local cash = player:FindFirstChild("Cash")

game.Players.PlayerAdded:Connect(function(player)
	while true do
		print("Loading..")
		wait(5)
		local cash = player:FindFirstChild("Cash")
		cash.Value = cash.Value + 5
		
	end
end)

I have never encountered this issue, and have no idea what’s wrong. Any help is appreciated!

1 Like

This is your issue, Your defining your cash variable before the player added event. , And your making 2 variables with the same name which will also mess up your script and intention

 game.Players
local cash = player:FindFirstChild("Cash")

game.Players.PlayerAdded:Connect(function(player)

Thank you for pointing that out! That does not seem to fully fix the issue because dev products do the same thing. I believe it is in the issue with the 100 cash at the beginning. Do you see anything wrong with that data store script?

I recommend putting this script into your data store script. To do this just do:

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	
	local tableToSave = {
		player:FindFirstChild("Cash").Value
	}
	
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end


game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	local cash = Instance.new("IntValue", player)
	cash.Name = "Cash"
	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
		
	end)
	
	if success then -- If there were no errors and player loaded the data
		
		cash.Value = data[1]
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!")
		cash.Value = 100 --Default value on first join. Change to whatever
	end
	
	while wait(5) do
		cash.Value = cash.Value + 5
	end	
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)
	
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)
		
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Just a tip, you should store your cash value into a folder inside the player called leaderstats. Try looking up some tutorials on youtube to help you understand datastore better.