.Changed: not running the function when int is changed in a dataStore

I am trying to link a leaderstat to a data store value but for some reason this function wont run when the integer for the leaderstat is changed. (.Changed:). I know the gold variable is defined correctly because the line before it runs perfectly fine. Any help?

local function LoadData(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	

	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent = leaderstats


	local Success = nil
	local playerData = nil
	local attempt = 1
	
	repeat Success, playerData = pcall(function()
			return Database:GetAsync(player.UserId)
		end)
		attempt += 1
		if not Success then
			warn(playerData)
			task.wait(3)
		end
	until Success == true or attempt >= 5
	
	if Success  then
		
			print("Data!!!!!", playerData)
			
		if not playerData then
			print("Creating new data...")
			playerData = {
				["Gold"] = 10
			}
		end
		SessionData[player.UserId] = playerData
	else
		warn("Unable to get data")
		player:Kick("Data loading issue. Try again later.")
	end
	gold.Value = SessionData[player.UserId].Gold --- THIS WORKS CONFIRMED
	gold.Changed:Connect(function() --- this does NOT fire when the int is changed
		print("CHANGED")
		SessionData[player.UserId].Gold = gold.Value
	end)
	leaderstats.Parent = player
end

If the gold.Value = ... works, could you try using:

gold:GetPropertyChangedSignal("Value"):Connect(function()

and see if that fires for Changed?

1 Like

Same thing, doesnt fire. I tried checking to see if .changed works in a local script if it is defined differently like this

local Gold = game.Players.LocalPlayer:FindFirstChild("leaderstats").Gold
Gold.Changed:Connect(function()
	print("Changed elsewhere")
end)

And it DOES print this out. However, when I tried to define the gold value in the same way on the server script (player value is passed through) it didnt work.

1 Like

Could the function being inside of the PlayerAdded() function cause it to not work? I was watching a video to better understand datastores and it worked fine for them but im not sure what else would cause it.

Ah, I did some testing and I see where you went wrong. This is because you’re not changing it for the server, you’re changing it on the client. Changes on the client only show for you, and not the server. Next to the stop button in studio, it’ll say “current client”, you can click that and change to the server’s view and make changes there that will replicate, to go back hit the button again.

Oh yep. The way I was testing it was through a local change. Thank you very much for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.