Hatching 3 eggs makes the pets lag a bit compared to hatching one egg

local i  = 0
	
	while i < 180 do
		petModel:SetCFrame(CFrame.Angles(0,i/18 / math.pi * 2 % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10),0,0))
		petModel2:SetCFrame(CFrame.Angles(0,i/18 / math.pi * 2 % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10),0,0))
		petModel3:SetCFrame(CFrame.Angles(0,i/18 / math.pi * 2 % (math.pi * 2), 0) * CFrame.Angles(math.rad(-10),0,0))
		i += 1

		task.wait()
	end

When I hatch one egg, it is buttery smooth, but when I hatch 3, the pets are going at like 30 fps. I believe it is because of this code but I don’t know how to optimize it.

Do the calculation once and then apply it three times, and maybe do some pre-calculations outside the loop and remove the task.wait() because this will only run for 180 iterations, like this:

local i = 0;
local FIXED = CFrame.Angles(math.rad(-10),0,0);
local MODULO = 2 % (math.pi * 2);
while i < 180 do
	
	local cframe = CFrame.Angles(0,i/18/math.pi * MODULO,0) * FIXED;
	petModel:PivotTo(cframe);
	petModel2:PivotTo(cframe);
	petModel3:PivotTo(cframe);
	i += 1;
end

I’m sure there is another shortcut on the second CFrame.Angles() but it eludes me.

Not sure if this’ll work (or if it’s efficient), but worth a try.

You could also pre-calculate all these CFrames and store them in a table since there are only 180 of them and just iterate that using a for i=0,180 do loop instead of a while loop. You would have to benchmark which performs the best of course.

Why remove the wait? without the wait it just goes for like 1/100 of a second then dissapears, I wanted the effect to be about 3 seconds thats why the wait was there

No problem, keep it in if required. Did this in anyway improve the performance of the original code?

How about the table iterator with the pre-calculated CFrames? Did this offer any improvement to the original?

Not much, if any. Also PivotTo didnt work so I changed it to SetCFrame