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.
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.
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.
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.
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.