How to format meshes into a grid?

What I’m trying to do is get my pets (the ones that follow me around) to format themselves into a 3-pet-wide grid. Currently I’ve managed to make them do this:
image

Using this code:

for i, pet in pairs(pets) do
		if pet and human and torso then --detect if player is existant
        	if human.Health >= 1 then --detect if the player is alive
				if (pet.PrimaryPart.Position - char.PrimaryPart.Position).Magnitude >= 2.5+(i*3) then --distance between oldpos and currentpos must be greater than 2 studs, otherwise this will not update
            		local cf = torso.CFrame * CFrame.new(3-count,2,2+(i*3)) -- subtracting 3 from count gives it x offset, y is irrelevant, and z puts them out into rows
            		pet.PrimaryPart.BodyPosition.Position = Vector3.new(cf.x,cf.y,cf.z) --moves them into position
					if count >= 3 then--detects if it's reached end of row
						count = 0
					else
						count = count+1
					end
				else
					pet.PrimaryPart.CFrame = pet.PrimaryPart.CFrame*CFrame.Angles(0,0.016,0) -- make them spin a bit when idle, irrelevant
				end
				oldpos = char.PrimaryPart.Position --then set oldpos to current position
        	else
				wait()
			end
		else
			wait()
    	end
	end

Edit 1: Added some comments to make life interesting
(Handled in the client if you’re interested)
How would I make them align into 3-wide horizontal rows?

1 Like

So right off the bat, a few code smells. I know it is not what you are asking but I couldn’t help myself. You can easily just check

if pet and character then

and that you are using wait() excessively. Use return instead! There’s a whole article about not using wait() Avoiding wait() and why

Anyway, to answer your question, it is quite simple

just use two for loops (or one, I haven’t tried that)

for i = 1, grid, 1 do
for v = 1, grid, 1 do
local pet = blah blah
pet.Position = Vector3.new(pet.Size.X * i, height, pet.Size.Y * v)
end
end

Now, you will probably have to use CFrame to make it relative to the character but I trust you are going to be able to do the rest!

2 Likes

Could you please rephrase that?
(I mean, I get the essence of what you’re trying to get across)
Because just giving me the new code doesn’t help if I don’t know what it’s supposed to do

I just re-read what I wrote! I am terrible for not expressing myself properly! I was in a rush.

Anyway:

for i = 1, grid1, 1 do
for v = 1, grid2, 1 do
local pet = blah blah
pet.Position = Vector3.new(pet.Size.X * i, height, pet.Size.Y * v)
end
end

So the first “grid1” in the first for loop will determine the column size (how many pets can there be vertical) and the second will decide how many can be horizontal. Now, if you want a gap in between these pets, you will have to tweak it.

If you want to see how the general idea of this script works.
Just paste this script in and press play

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

for i = 1, 5, 1 do
for v = 1, 5, 1 do
local pet = Instance.new("Part", workspace)
pet.Anchored = true
pet.Size = Vector3.new(2,2,2)
game:GetService("RunService").Heartbeat:Connect(function()
pet.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(pet.Size.X * i, 3, pet.Size.Y * v)
end)
end
end

I haven’t tested it s0, good luck.

This script generates parts, I was originally going to take them from a table.
How would I adjust this code to work from a table of parts?

Alright, the code will get ugly xd but ADD ANOTHER FOR LOOP!

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local petTable = {}
for i = 1, 5, 1 do
for v = 1, 5, 1 do
for _, v in pairs(petTable)
local pet = v
game:GetService("RunService").Heartbeat:Connect(function()
pet.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(pet.Size.X * i, 3, pet.Size.Y * v)
end
end)
end
end

Uh, no (using a table consisting of standard parts)
image