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:
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?
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
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