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

Moving multiple parts at the same time?

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

3 answers

✓ Accepted answer

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!

3 comments

0

I'd like it to slide to the position though.

0

Oh, sorry

0

You can repeat this script, change increment by .1 at a time, it may not work but should

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

1 comment

0

A better solution is to just turn the loops around and repeat the whole thing 50 times instead of just the inside.

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