Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Changed or GetPropertyChangedSignal()? Which one is better?

Asked by 3 years ago

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!

0
answered iuclds 720 — 3y
0
Just to clarify there's pretty much no difference between the two functions. You might as well use :GetPropertyChangedSignal JesseSong 3916 — 3y

2 answers

Log in to vote
1
Answered by
iuclds 720 Moderation Voter
3 years ago
Edited 3 years ago

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.

1
I see now. It's better to GetPropertyChangedSignal because it checks for  the specific value property exists and is changed. Dovydas1118 1495 — 3y
0
Thank you for answering it, I will surely use GetPropertyChangedSignal(). Dovydas1118 1495 — 3y
Ad
Log in to vote
-2
Answered by 3 years ago
Edited 3 years ago

The GetPropertyChangedSignal since in general it just works better than .Changed

0
But why though? Dovydas1118 1495 — 3y
0
I want to know why Dovydas1118 1495 — 3y
0
it's faster and more precise in my opinion aiming4u 61 — 3y
0
to add on to that, i see it as like using a service instead of a while loop ^ aiming4u 61 — 3y
0
this is disgusting. pity to you iuclds 720 — 3y

Answer this question