Checking if a property from a table of parts has been changed

I am currently trying to create a visor for a player to see through. Basically by changing colors and transparency of bricks (as well as a few other small environment manipulations) I can create the effect I am looking for. However, it is crucial that I know when a part’s color or transparency has been changed so that the game knows what color the part should be when the visor is turned off.

I want to know if there is an easier way to wait for a property change of a table of parts. This is what I currently have:

for i, v in pairs(AllParts) do
	v:GetPropertyChangedSignal("Transparency"):Connect(function()
		UpdateColors1(v)
	end)
	v:GetPropertyChangedSignal("BrickColor"):Connect(function()
		UpdateColors1(v)
	end)
	v:GetPropertyChangedSignal("Material"):Connect(function()
		UpdateColors1(v)
	end)
end

The problem is that “AllParts” is not a static table and I have noticed that this loop only considers the values of the table when it first runs.

It is also a problem to call the loop again after the table has been updated because there seems to be no way to get it to stop waiting for the stuff from the first time in ran. In other words, I would be getting a double dose of “UpdateColors1()”

I’m not sure if it is the best approach for many reasons.
When I worked on sg like this I added some values to each modified part on change, storing the important original values of color, size, transparency, orientation etc.
In case of few parts, maybe the best way to make a clone before you change the part and store the clone as an objectvalue parented to ReplicatedStorage or anything but Workspace.

When you want to turn off the effect, loop through all parts and revert the values from the stored values, and you can even destroy the temp. values and objects.

I hope it was clear :slight_smile:

Consider using CollectionService instead. To add something to a collection, use CollectionService:AddTag(string tag, Instance inst), where tag would be a string for the name of your collection essentially.

You can then use CollectionService:GetInstanceAddedSignal(string tag) to listen for when an instance is added to the specified collection. Its complement exists too – :GetInstanceRemovedSignal(string tag)

1 Like

This works when none of the parts change color when the visor is on. But say a part changes color while the visor is on. I want to instantly change that part to the color it should be while the visor is on and save the new color so that when the visor is turned off, it remembers what the new color is. This solution allows me to save the color before the visor is turned on, but not check when the color changes. When the visor is turned off, the part will revert back to the color that was saved and not the color that it should have changed to.

I have also played with having the server send the actual properties to the client but this slows my programs down too much.

@incapaxx
This is not quite what I am looking for either. I am trying to see when a property is changed. I could use CollectionService to hold all of my property data, but I still don’t have a way to check if those properties are changed.

Here is a visual example of my problem.

You want a “when a part is added to my table, listen for property changes”. Am I wrong?

:GetInstanceAddedSignal would let you do just that.

CollectionService:GetInstanceAddedSignal("YourTag"):Connect(function(new_instance)
    -- new_instance points to the new instance added to the collection 
end)
1 Like

@incapaxx
Not exactly. I already have a table full of parts. I have more code set up to add and remove incidences from the table but that is not the part I have a problem with. I need to know when a part that is already in the table changes it’s color or transparency properties.

@incapaxx
The code I have above works except that it doesn’t take into account any indices that may have been added to the table.

Yes, that is exactly why I recommend collection service here. So you can easily listen for new parts being added to the collection. As for listening for property changes, you already have a reference to the new instance, so you would do the connections within the listener

If you really need more pseudocode…

CollectionService:GetInstanceAddedSignal("YourTag"):Connect(function(new_instance)
    -- new_instance points to the new instance added to the collection
    new_instance:GetPropertyChangedSignal("property"):Connect(function()
    end)
end)
2 Likes

Ah, I see now. I misunderstood at first. I will have to undo several things I’ve already done but this should fix my issue.