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
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