Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Changed or GetPropertyChangedSignal()? Which one is better?

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!

2 comments

0

answered

0

Just to clarify there's pretty much no difference between the two functions. You might as well use :GetPropertyChangedSignal

2 answers

✓ Accepted answer

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.

2 comments

1

I see now. It's better to GetPropertyChangedSignal because it checks for  the specific value property exists and is changed.

0

Thank you for answering it, I will surely use GetPropertyChangedSignal().

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

5 comments

0

But why though?

0

I want to know why

0

it's faster and more precise in my opinion

0

to add on to that, i see it as like using a service instead of a while loop ^

0

this is disgusting. pity to you

This discussion is preserved as it was. New answers, comments, and votes are closed.