On IntValues, I've seen people talk about using either IntValue.Changed
or IntValue:GetPropertyChangedSignal
they both work the same way (as far as I see it) and I wanna know what's the difference between the two.
game.ReplicatedStorage.IntValue:GetPropertyChangedSignal():Connect(function() --Do stuff end)
And the Changed event
game.ReplicatedStorage.IntValue.Changed:Connect(function() --Do stuff end)
I wanna know which one is better, and for what reason. Any help is appreciated!
Changed,
Changed is used mainly for instances that usually have just 1 property, for instance, a BoolValue or a NumberValue. However, there can be some false positives such as when the name is changed and you're only trying to know when another property is changed. Here's some example code, demonstrating how to use Changed incorrectly and correctly. I wouldn't use changed in any of my new work since it seems very inefficient compared to GetPropertyChangedSignal.
local V = Instance.new('BoolValue') V.Changed:Connect(function(Property) print('Value changed to '..V.Value) end) V.Value = true -- this will trigger the function and print 'true' V.Name = 'false positive' -- this will trigger the function and print 'true' incorrectly since we aren't checking the property V.Value = false -- this will trigger the function and print 'false' -- the parameter 'Property' isn't being used, therefore we aren't checking the property which is dumb.
This code above is wrong in many ways. False positives are silly, and we can do better than that.
local V = Instance.new('BoolValue') V.Changed:Connect(function(Property) if Property == 'Value' then print('Value changed to '..V.Value) elseif Property == 'Name' then print('Name changed to '..V.Name) end end) V.Value = true -- this will trigger the function and print 'Value changed to true' V.Name = 'good positive' -- this will trigger the function and print 'Name changed to good positive' since we are checking the property, which is good practice. V.Value = false -- this will trigger the function and print 'Value changed to false' -- the parameter 'Property' is being used, a much better way than the other code.
GetPropertyChangedSignal, Now, GetPropertyChangedSignal is much better since it checks for only a specific property. It's pretty straight forward, lol, so here's an example on how to use it.
local V = Instance.new('IntValue') V:GetPropertyChangedSignal('Value'):Connect(function() print('Value changed to '..V.Value) end) V.Value = 8 -- prints 'Value changed to 8' since we are checking for the value to be changed V.Name = '6ix9ine sucks xd'-- prints nothing because we aren't checking for a name change, we are checking for the value change V.Value = 7635 -- prints 'Value changed to 7635' since we are checking for the value to be changed
No problem.
The GetPropertyChangedSignal since in general it just works better than .Changed