How to detect property change in many Instances

Hi, I wanted to know if anyone knows how to use the event :GetPropertyChangeSignal

		DetectedInsideTrainSpeaker.Value:WaitForChild("NextYuzuwa"):GetPropertyChangedSignal("Playing"):Connect(function()
			UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, "NextYuzuwa")
		end)
		DetectedInsideTrainSpeaker.Value:WaitForChild("NextVictoria"):GetPropertyChangedSignal("Playing"):Connect(function()
			UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, "NextVictoria")
		end)
		
		DetectedInsideTrainSpeaker.Value:WaitForChild("ApproachYuzuwa"):GetPropertyChangedSignal("Playing"):Connect(function()
			UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, "ApproachYuzuwa")
		end)
		
		DetectedInsideTrainSpeaker.Value:WaitForChild("Test"):GetPropertyChangedSignal("Playing"):Connect(function()
			UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, "Test")
		end)

Everything works fine, however, as i develop the game, they would be like 50 over sounds and repeating that line of code 50 times would work but i wanted to see if there are any better solutions.

It needs to spawn a task and send its name as a arguement as well

1 Like

If you are only changing the string value for each block of code, you could just put all the string values in an array, iterate through it, and input the value inside the WaitForChild and task.spawn parameters.

Something like this

for i,v in ipairs(array) do
	DetectedInsideTrainSpeaker.Value:WaitForChild(v):GetPropertyChangedSignal("Playing"):Connect(function()
		UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, v)
	end)
end


1 Like

What about detecting property changing in multiple of instances? Else i would have to rewrite this line of code repeatedly:

DetectedInsideTrainSpeaker.Value:WaitForChild(v):GetPropertyChangedSignal("Playing"):Connect(function()
1 Like

Not tested but it should work

local myArray = {} -- contains each instance and their properties
local function insert(...)
	-- ... is a placeholder for all arguments that are passed
	-- {...} essentially puts all arguments in a table so we can iterate through them
	for i,v in {...} do
		table.insert(myArray,v)
	end
end

local prop1 = {Instance.new("ObjectValue"), "Test", "Something", "Cool"} -- individual container
local prop2 = {Instance.new("ObjectValue"), "Fly", "Flying", "Superman"}

insert(prop1, prop2)

local function UpdateSpecificSounds(object: Instance, property: string) -- your function with objectValue added as parameter to get the instance
	
end

local UpdateSpecificSoundsTask

for i,array in ipairs(myArray) do
	-- starts from 2 to the end of the second array (because first index is the Instance)
	local object: Instance & any = array[1] 
	for i = 2,#array do
		local property = array[i]
		
		object.Value:WaitForChild(property):GetPropertyChangedSignal("Playing"):Connect(function()
			UpdateSpecificSoundsTask = task.spawn(UpdateSpecificSounds, object, property)
		end)		
	end	
end

You would still need to change it to work with your intentions but i think you get the idea

1 Like

yes there is definitely a better way to do this.

use a for loop

For i, children in pairs(DetectedInsideTrainSpeaker:GetChildren() do
      --then you can you can do everything just with the children variable!
     --so if you were to do print(children) it would print a list of those parts!
end
1 Like