How do I anchor a model with a script?

So I’m currently working on a game in Roblox Studio and bumped in to some issues when I forgot to anchor all my TONS of parts in the models I use. I would take me hours upon hours to anchor all of these one by one and also it would be very messy.

So instead of all of that, I tried to anchor it with a script. But I couldn’t do it… i’m new to scripting so I thought any of you people here might know? Basically I need a script that can anchor all my parts in the model. There are grouped parts inside the model aswell (just adding).

Thanks - ayohbs

9 Likes

You could use :GetChildren to get all of the models children with just 1 script.

local EntireModel = workspace.Model:GetChildren()

for i,v in pairs(EntireModel) do
  if v:IsA("BasePart") then
  v.Anchored = true
end
 
12 Likes

I tried the script, and the outcome was… well nothing? Strange, I thought of this aswell but when it didn’t work I thought I was wrong. There’s no error output of some sort and nothing is still anchored.

4 Likes
local children = game.Workspace.Model:GetChildren() --Replace "Model" with the name of the model.

for i, child in ipairs(children) do
    if child:IsA("BasePart") then
        child.Anchored = true
    end
end
7 Likes

Is there a reason for needing a script to do this? In workspace if you click the model you can then click the anchor button at the top of your screen.

10 Likes

As a noob, I have to take this stupid fail of mine to admit. You’re right! Sorry for my lack of knowledge in the studio.

2 Likes

its ok we all make mistakes, it is good to learn how to make a script for this though, especially for local parts.

4 Likes

you can type: script.Parent.Anchored = true

2 Likes

Models don’t have an anchored property, this won’t work. You’ll need to iterate through the model’s descendants and anchor anything that’s of the class BasePart. If you aren’t doing this at run time though then there’s no need to write any code when you can just use the anchor tool in Studio.

3 Likes

yes i am know, he can only use this for meshpart or model with welds. (about the anchor option on studio is right but he want with script)

1 Like

The purpose of being supplied the script is just to automate the task as per the OP that they have many models that would need to be anchored. Iteration would be required in order to get BasePart instances to anchor them.

I’m not too sure I understand what you’re replying with otherwise? Could you clarify please? :slightly_smiling_face:

1 Like

Technically the time you would need a script to anchor a model/part(s) is if it is a local model/part(s), or in a case @colbert2677 has explained. Otherwise it is a lot easier and more clear to use the built in anchor feature roblox gives us. That is why I told @ayohbs it was not necessary for a script to do this work, it would be a waste of time.

2 Likes

Could it have been that the children were already destroyed before the script anchored it?

I am not sure, but the thing is I solved it with Hello42bacons help. I’m new to roblox studio so clicking the Anchor button was all it took.

local function r(p)
	for _, v in pairs(p:GetChildren()) do
		if v:IsA'BasePart' then
			v.Anchored = true
		end
		r(v)
	end
end
r(workspace)

This script anchors everything your map just in case you need it.

1 Like

You could greatly improve that code-block for recursive anchoring. :GetDescendants() looks like a much better usecase than recursively iterating over :GetChildren().

local function anchor(object)
	local descendants = object:GetDescendants()

	for i, descendant in pairs(descendants) do
		if descendant:IsA("BasePart") then
			descendant.Anchored = true
		end
	end
end

anchor(workspace)
4 Likes