So i want to make every part in my model move. I've tried to write a basic script and all it does is move 1 part at a time to the location, and then grabs the second part and moves it so the location (and so on...). All my parts are anchored and I want it to slide to the position.
This is what I have
p = script.Parent:GetChildren() for i = 1,#p do if p[i]:IsA("Part") then --print(p[i].Name) for z = 1,50 do wait() p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1) end end end
Since you're using wait
, each iteration in the loop has to complete before the next one. To make this not an issue, put that loop on the outside.
p = script.Parent:GetChildren() for z = 1,50 do wait() for i = 1,#p do if p[i]:IsA("Part") then p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1) end end end
This is much more simple my friend!
local model = script.Parent model:MoveTo(0,0,0)-- change to coordinates
If this helped you, please accept and/or upvote! Thank you!
Well, when you wait() for the brick to move, it yields the entire for loop so that it won't loop it again until it's finished. The solution is coroutines.
Coroutines are like regular functions, except they run on a different thread -- That is, wait()s and other types of delays will only yield the coroutine, not the entire function, allowing all bricks to slide at once. Something like this:
p = script.Parent:GetChildren() for i = 1,#p do if p[i]:IsA("Part") then --print(p[i].Name) local moveBricks = coroutine.wrap(function() for z = 1,50 do wait() p[i].CFrame = p[i].CFrame + Vector3.new(0,0,0.1) end end) moveBricks() end end