How to select objects with the same name with GetChildren?

well, i have a problem with this. I already have an idea of ​​how to do it, but I want it to be activated in a more optimized way and with a Remote Event, the following script already does it as I want as I want but not in an optimized way:

local Presionar = script.Parent

Presionar.MouseButton1Click:Connect(function()
	local Model = game.Workspace.Arboles:GetChildren()

	for i, child in ipairs(Model) do
		if child and child:IsA("Model") then -- Change "Part" to the name which will trigger the line below
			child.Hojas.Material = Enum.Material.Plastic
		end
	end
end)

I want to apply it as an optimized way like:

local Arboles = game:GetService("Workspace"):WaitForChild("Arboles")
local FireChangeTreeScript = ReplicatedStorage.Graficos:WaitForChild("FireChangeTreeScript")

local TreeTextureProfiles = {
	["Disable"] = {["Hojas"] = {["Material"] =  Enum.Material.Plastic,}, {["Tronco"] = Enum.Material.Plastic,},},
	["Enabled"] = {["Hojas"] = {["Material"] =  Enum.Material.Plastic,}, {["Tronco"] = Enum.Material.Plastic,},},
}

local function ChangeTreeSingal(newThreeValue)
	print(newThreeValue)
	for _,obj in next,Arboles:GetChildren() do -- it would be better if you compiled all your PointLights into table, to avoid massive iteration through everything in Workspace
		for property,value in next,TreeTextureProfiles[newThreeValue]["BasePart"] do
			obj[property] = value
		end
	end
end
FireChangeTreeScript.OnClientEvent:Connect(TreeTextureProfiles)

But it doesn’t work and I don’t know how to fix it

can you show the output of the script?

Try this?

local Arboles = workspace:WaitForChild("Arboles")
local FireChangeTreeScript = ReplicatedStorage.Graficos:WaitForChild("FireChangeTreeScript")

local TreeTextureProfiles = {
	["Disable"] = {
		["Hojas"] = {
			["Material"] =  Enum.Material.Plastic
		},
		["Tronco"] = {
			["Material"] =  Enum.Material.Plastic
		}
	},
	["Enabled"] = {
		["Hojas"] = {
			["Material"] =  Enum.Material.Plastic
		},
		["Tronco"] = {
			["Material"] =  Enum.Material.Plastic
		}
	},
}

FireChangeTreeScript.OnClientEvent:Connect(function(newThreeValue)
	print(newThreeValue)
	for _,obj in pairs(Arboles:GetChildren()) do
		for property,value in pairs(TreeTextureProfiles[newThreeValue][obj.Name]) do
			obj[property] = value
		end
	end
end)

It gives me the following error “Attempt to connect failed: Passed value is not a function

oh yeah, I didn’t put the script right. The true error is “invalid argument #1 to 'pairs' (table expected, got nil)

Assuming there are several models

local Arboles = workspace:WaitForChild("Arboles")
local FireChangeTreeScript = ReplicatedStorage.Graficos:WaitForChild("FireChangeTreeScript")

local TreeTextureProfiles = {
	["Disable"] = {
		["Hojas"] = {
			["Material"] =  Enum.Material.Plastic
		},
		["Tronco"] = {
			["Material"] =  Enum.Material.Plastic
		}
	},
	["Enabled"] = {
		["Hojas"] = {
			["Material"] =  Enum.Material.Plastic
		},
		["Tronco"] = {
			["Material"] =  Enum.Material.Plastic
		}
	},
}

FireChangeTreeScript.OnClientEvent:Connect(function(newThreeValue)
	print(newThreeValue)
	local Current = TreeTextureProfiles[newThreeValue]
	if not Current then	print("value not found")		return	end
	for _,obj in pairs(Arboles:GetDescendants()) do
		if not obj:IsA("BasePart") or not Current[obj.Name] then	continue		end
	
		for property,value in pairs(Current[obj.Name]) do
			obj[property] = value
		end
	end
end)
2 Likes