Can Value.Changed fire with nil even if the value isn't set to nil?

I have a block of code that goes like

myIntValue.Changed:Connect(function(newVal)	
	if newVal >= 25 then
            ...
	end
end)

according to Google Analytics this is occasionally throwing the error “attempt to compare nil and number.” I’m like 99% sure I never set the value to nil in any script. So can .Changed fire with nil?

1 Like

.Changed fires when ANY property of the Instance changes. To get an event that is fired ONLY when the Value property is changed
Edit: My bad, I got this wrong, credit to @sjr04 and @iiFusionzX for pointing this out. You can use .Changed, and you could also use :GetPropertyChangedSignal()

What you could do is add a check to ensure that the new value is not nil if you are using the .Changed event.


You could use :GetPropertyChangedSignal("Value"):Connect(...), like so:

function UpdatedValue()	
    local newVal = myIntValue.Value
	if newVal >= 25 then
            ...
	end
end

myIntValue:GetPropertyChangedSignal("Value"):Connect(UpdatedValue)

Hope this helps :slight_smile:


More info on :GetPropertyChangedSignal(): Instance | Documentation - Roblox Creator Hub

Incorrect, ValueBase instances (IntValue, BoolValue, etc) use a non-inherited Changed event which fires only on the Value property changing. Instead of the classic “string property name being passed” as an argument to the listeners, the behavior is that the new Value is passed to the listener

2 Likes

I believe when using .Changed on any type of Value Object, it only gives you the parameter of the new value.

1 Like

If that is so, my bad I should have checked. I had used this post to get my information from.

Thanks! I’ll just use that then. Out of curiosity, could any .Changed event for an IntValue fire with nil if I never set the value to nil?

This is me speculating, but maybe as a server is shutting down? No idea.

Is there a way to listen for a number being changed? My code is in a local script and currentExp gets its value from an IntValue that is called from the server script, which is then sent back to my local script

currentExp.Changed:Connect(function()

CombatBar:TweenSize(UDim2.new(currentExp / currentRequirement, 0, 0, 8), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)

end)

I get the error " attempt to index number with ‘GetPropertyChangedSignal’" when I used GetProperty and “attempt to index number with ‘Changed’” when I use .Changed

Is currentExp just a variable? You’ll probably want to do something like this instead.

local function changeExp(newExp)
    currentExp = newExp
    CombatBar:TweenSize(UDim2.new(currentExp / currentRequirement, 0, 0, 8), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 1, true)
end

--later, whenever you want to change currentExp
changeExp(currentExp+100) -- add 100 exp to currentExp
1 Like

Thank you so much, that works much more effectively than what I was using previously!!!

1 Like