When connecting to property changes, there’s multiple ways to do so including:
- Loops (While/Repeat/For)
But what’s the best way and how can I use it to the best standard?
Inparticular, loops are used the majority of the time however this is not the method to go about this.
Loops are inefficient especially when you have many, yielding - therefore requiring to be coroutine wrapped - and even slow (Sometimes the value might’ve changed a couple of frames before the loop gets to that).
On the other hand, Changed/GetPropertyChangedSignal don’t need any coroutine wrapping neither are they slow also you can even
:Disconnect() them to free up memory!
ValueBases and updating
ValueBases are used a lot in current day roblox development, they’re extremely helpful for sharing data or just keeping data there for use later on. Sometimes you might want to change a TextLabel or something UI related to show the current value - a lot of the time while loops are used for this and as I’ve said that is not the way to go. Lets say we wanted to show the amount of Coins we had in a TextLabel, a while loop would look like:
local plr = game:GetService("Players").LocalPlayer local stats = plr:WaitForChild("leaderstats") while wait() do script.Parent.Text = stats.Coins.Value end
As I’ve said before, this is really inefficient and you’re even updating it to the same thing it is a lot of the time - if you had many Values and these then it’d take up quite a bit of memory whereas using a Changed signal can reduce memory used:
local plr = game:GetService("Players").LocalPlayer local stats = plr:WaitForChild("leaderstats") stats.Coins.Changed:Connect(function(newval) script.Parent.Text = newval end)
Why not GetPropertyChangedSignal(“Value”) though? Using GetPropertyChangedSignal is not a problem, it actually really shows what you’re doing, however Changed is specially adapted for ValueBases as Changed will only fire for when the Value is changed also GetPropertyChangedSignal does have a minor difference in speed compared to Changed - this isn’t too much of a problem though. The api-reference talks about Changed and ValueBases here:
Waiting for a ValueBase’s Value to change
In this case, a repeat loop is usually used - now this is an exception as you do need the yielding and you can choose what to yield until however in a majority of cases these are BoolValues and you’re just waiting for it to change from false to true. Now here you can use a :Wait() on Changed, example:
Moving away from ValueBases
Now in other instances, GetPropertyChangedSignal will prove more useful.
Lets say we wanted to change a Frame’s BackgroundColor3 to a BasePart’s color.
Since Changed on everything else except ValueBases has the parameter of what property changed, you’d have to do:
BasePart.Changed:Connect(function(property) if property == "Color" then Frame.BackgroundColor3 = BasePart.Color end end)
Whereas, with GetPropertyChangedSignal you can easily just do:
BasePart:GetPropertyChangedSignal("Color"):Connect(function() Frame.BackgroundColor3 = BasePart.Color end)
Sometimes you may need the Changed event to connect on many different properties however in this instance GetPropertyChangedSignal is the way to go.
Mistakes with GetPropertyChangedSignal
Occassionally, I’ve seen mistakes with GetPropertyChangedSignal due to it’s different syntax - one of which like:
Instance:GetPropertyChangedSignal("Property") do end
Everyone makes mistakes, just remember GetPropertyChangedSignal acts like an event therefore you need to Connect/Wait on it.
Going back to what I said about freeing up memory again, you can do so like this:
local c c = Instance.Changed:Connect(function() print("Hello world!") c:Disconnect() end)
Thanks for reading!
This was my first community tutorial, I hoped you enjoyed.
If you have anything to add, please be sure to say so in the replies!
Also correct me if I’m wrong on anything