Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I unanchor all models in my game?

Asked by 4 years ago

I am trying to write a script that unanchors all models in my game. Here is my current code:

for _,unanchor in pairs(game.Workspace:GetChildren()) do
    if unanchor:IsA("Model") then
            unanchor.Anchored = false
    end
end
0
I dont think you can unanchor the models. If you wanted to unanchor everything in the game you would have to loop through all the children of the models as well VoltiCoio 19 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

You cant anchor a model so we would go through all its children instead

for _,unanchor in pairs(game.Workspace:GetChildren()) do
    if unanchor:IsA("Model") then
    for e, r in pairs(unanchor:GetChildren()) do --goes through the models children
        if r:IsA("BasePart") then --checks if its a part
            wait()
            r.Anchored = false --unanchors it it
            end
    end
    end
end
Ad
Log in to vote
0
Answered by
bum5Br 97
4 years ago
Edited by royaltoe 4 years ago

I believe you're not able to unanchor or anchor models, only parts, and for such you would have to get the children of the models to unanchor each part inside it. For that, you would simply need to use GetDescendants() instead of GetChildren() !

for _,unanchor in pairs(workspace:GetDescendants()) do
    if unanchor:IsA("BasePart") then --checks if child is a type of part
            unanchor.Anchored = false --unanchors it
    end
   wait(1)
end

Tips : You can use workspace instead of game.Workspace, it's easier. I would also advise keeping the wait(), since it keeps your game from crashing or having insane lag spikes if the part count is high. And remember this will unanchor EVERYTHING! , it doesn't check if the part is the Baseplate, or if its locked or anything! for that you would need to check either it's name or locked property!

Hope this helps!

Alternatively, if you want to unanchor every model in the workspace, not every part:

for _, child in pairs(game.Workspace:GetChildren())do
        if child:IsA("Model") then 
                for _, desc in pairs(child:GetDescendants())do
                        if desc:IsA("BasePart") or desc:IsA("Union operation") then 
                                desc.Anchored = false
                        end
                end

        end
end

Answer this question