Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I unanchor all models in my game?

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

1 comment

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

2 answers

✓ Accepted answer

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.