NumberValue change not being detected or not firing function

Hello, I have ran into a problem where my script does not fire the function when the numberValue (moneyValue) is changed.
Here is the script:

script.Parent.Triggered:Connect(function(player)
	local playerName = game.Players:FindFirstChild(player.Name)
	local leaderstats = playerName:FindFirstChild("leaderstats")
	local moneyValue = leaderstats:FindFirstChild("Money")

	if moneyValue and moneyValue:IsA("NumberValue") then
		local function moneyChanged()
			print("moneyValue has been changed")
		end

		moneyValue.Changed:Connect(moneyChanged)
	else
		print("MoneyValue not found")
	end
end)

The problem lies in the line: moneyValue.Changed:Connect(moneyChanged), as the function will print if called, but when it checks if the value is changed, neither an error is printed or it does not detect change in the value, resulting in “moneyValue has been changed” not being printed.

Thanks!

Use GetPropertyChangedSignal instead of Changed.

moneyValue:GetPropertyChangedSignal("Value"):Connect(moneyChanged)

Maybe you can initialize your function too?

local function moneyChanged()
	-- Update somethings
	print("moneyValue has been changed")
end

moneyChanged() -- Initialize

moneyValue:GetPropertyChangedSignal("Value"):Connect(moneyChanged)

Make sure this doesn’t trigger multiple times because otherwise you will have multiple connections doing the same thing and that creates memory leaks.

1 Like

Changed is the optimal event for ValueBase objects.

@KireiKage
Can you confirm it is a NumberValue and not any other type?

2 Likes

Thanks for replying, when the moneyChanged function is called, in the initialization, it does print that the value has been changed, however, checking the value in the print and calling only returns 0 in the output, the starting value. If the value has been changed, it still recognizes it as 0, despite being whatever other number. Without initializing the function as well, nothing is printed, not even that the value is 0, resulting in the same problem I am having.

Changed gives the new value as a parameter, so try printing the aforementioned parameter and see what it prints.

ValueBase.Changed:Connect(function(newValue)

end)
1 Like

If you mean confirm in the script, it does check in the if statement of if moneyValue and moneyValue:IsA("NumberValue") then, unless you mean in another way. If you mean as the value itself (through properties), in game, I can see that it is of the class “NumberValue” and when creating it through an instance, NumberValue is used as the class.

You change the value on the client or the server side?

1 Like

The “Money” object is created through the server side (leaderstats) and the value is given from the client, once given from the client, the server detects change in the object, not the client side script that gives the values.

If the client-side changes the value, the server does not see anything change.

1 Like

Thank you! I thought that the server was able to see it as long as it was in the value property of the NumberValue and not accessed from a script.

This is how filtering works. Server-side changes replicate to clients. However clients don’t replicate to the server nor other clients.

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