How to unanchor every part in a model at the same time

Im trying to make a basic for i v pair script, I wanna unachor all the parts of a model at the same time using for i v pair loops since im still trying to learn it.

‘local Parts = {game.Workspace.Model:GetChildren()}
for i , v in pairs(Parts) do
v.Anchored = false
end’

7 Likes

You don’t need to cover game.Workspace.Model:GetChildren() in brackets since the function itself return the children instances in a table so you don’t have to.

local Parts = game.Workspace.Model:GetChildren()
for i , v in pairs(Parts) do
    v.Anchored = false
end’
10 Likes

To add on if you need them all to anchor at the same time this should work

local Parts = game.Workspace.Model:GetChildren()
for i , v in pairs(Parts) do
     spawn(function()
          v.Anchored = false
     end)
end
1 Like

Or in difference to the comments above,

You might need to unanchor/anchor all parts inside parts inside the model aswell, in which case:

local Parts = game.Workspace.Model:GetDescendants()
for i , v in pairs(Parts) do
   v.Anchored = false -- or true
end
4 Likes

You should disregard this post and the post I’m replying to, because unanchoring all the parts would be instant anyways (and spawning new threads would take just as long, if not longer)

Old post

Note: This isn’t true. spawn doesn’t yield the thread its called in. Ignore this post.

If the model has a lot of parts, this could actually take quite a while because spawn() has a wait() built into it.

If you need to unanchor every part at the exact same time, you should use the coroutine library instead.

local Parts = game.Workspace.Model:GetChildren()
for _,v in pairs(Parts) do
     coroutine.wrap(function()
          v.Anchored = false
     end)() -- coroutine.wrap() returns a function, these extra parenthesis call the returned function
end
6 Likes