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).
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.
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
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.
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?
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.
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)