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
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
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
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