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

Why is my script CFraming one by one?

I have a Model and inside are many bricks and a script at the root of the Model. I want it to CFrame all bricks in the model at the same time, this script is only doing it one by one, why?

while true do wait(4)
	for i, v in pairs(script.Parent:GetChildren()) do
		for i = 1, 25 do
			v.CFrame = v.CFrame*CFrame.new(0,-0.95,0)
			wait()
		end	
wait(4)
		for i = 1, 25 do
			v.CFrame = v.CFrame*CFrame.new(0,0.95,0)
			wait()
		end	
	end	
end 




1 answer

✓ Accepted answer

It is the way you have the loops, You loop with 'each' part then for this part you then set its CFrame. All you need to do is swap the loops around, it is also best if you get a list of the parts as we use it twice:-

while true do
	wait(4)
	local parts = script.Parent:GetChildren() -- gets the array of parts
	
	for i=1, 25 do
		for _, v in pairs(parts) do
			 v.CFrame = v.CFrame*CFrame.new(0,-0.95,0)
		end
		wait() -- wait after we have set all parts
	end

	wait(4)

	for i=1, 25 do
		for _, v in pairs(parts) do
			 v.CFrame = v.CFrame*CFrame.new(0,0.95,0)
		end
		wait() -- wait after we have set all parts
	end
end

You also will be able to remove the duplicate code here as they do the 'same' thing but with different data(Hint:- function). I hope this helps please comment if you do not understand how / why this code works.

1 comment

0

I think I understand the difference now in why I had to swtich it around.

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