How do I anchor/unanchor an entire model with models within it?

How do I anchor an entire model with models within it?

I am making a boat with an anchor function, that whenever you press the button. It anchors/un-anchors at will, here is the script:

script.Parent.ClickDetector.MouseClick:Connect(function()
	print("Anchoring")
	for _, v in pairs(script.Parent.Parent.Parent:GetChildren()) do
		v.Anchored = not v.Anchored
	end
end)

Here is a photo of the model:


I want it so that it checks all children of the model itself. And Check if the models contain parts/models etc.

Please, help would be appreciated.

1 Like

You can iterate through the models descendants and check if that descendant is a base part then anchor it accordingly.

script.Parent.ClickDetector.MouseClick:Connect(function()
	print("Anchoring")
	for _, descendant in pairs(script.Parent.Parent.Parent:GetDescendants()) do
		if (descendant:IsA("BasePart")) then
          descendant.Anchored = (not descendant.Anchored) 
        end
	end
end)
1 Like