How to define a child of a part thats named a number?

Hello! So I’m just going to let the image and embed speak for its self but i need help defining the TextureID of a mesh block.

local click = script.Parent.Parent:WaitForChild("ClickDetector")
local flag = script.Parent.Parent.Flag

click.MouseClick:Connect(function()
	flag["1"].TextureID = "rbxassetid://81958443"
	flag["2"].TextureID = "rbxassetid://81958443"
	flag["3"].TextureID = "rbxassetid://81958443"
	flag["4"].TextureID = "rbxassetid://81958443"
	flag["5"].TextureID = "rbxassetid://81958443"
	flag["6"].TextureID = "rbxassetid://81958443"
	flag["7"].TextureID = "rbxassetid://81958443"
	flag["8"].TextureID = "rbxassetid://81958443"
	flag["9"].TextureID = "rbxassetid://81958443"
	flag["10"].TextureID = "rbxassetid://81958443"
	flag["11"].TextureID = "rbxassetid://81958443"
	flag["12"].TextureID = "rbxassetid://81958443"
	flag["13"].TextureID = "rbxassetid://81958443"
	flag["14"].TextureID = "rbxassetid://81958443"
	flag["15"].TextureID = "rbxassetid://81958443"
	flag["16"].TextureID = "rbxassetid://81958443"
	
end)

I’m not exactly sure what you mean by “How to define a child of a part that’s named a number”, but your code could be simplified in the following ways:

if there are specific number-named children that you need to set the texture for, and some you don’t:

local NumChildren = 16
click.MouseClick:Connect(function()
	for i = 1, NumChildren do
		local theFlag = flag:FindFirstChild(tostring(i))
		if (theFlag) then
			theFlag.TextureID = "rbxassetid://81958443"
		end
	end
end)

or if all the children of flag contain the mesh parts you want to change, you should do this instead:

click.MouseClick:Connect(function()
	for i, child in ipairs(flag:GetChildren()) do
		if (child:IsA("MeshPart")) then
			child.TextureID = "rbxassetid://81958443"
		end
	end
end)
1 Like