BodyPosition has been deprecated so shouldn’t be used for new work, see documentation here BodyPosition.
Try this:
local row_count = 4;
local spacing = 2;
local start = -(row_count/2)*spacing;
local x_off = start;
local z_off = 5;
local count = 0;
for _, pet in pairs(pets) do
if count > 0 and count%row_count == 0 then
z_off += spacing;
x_off = start;
end
-- use x_off and z_off in BodyPosition.Position
x_off += spacing;
count += 1;
end