Multiple pets/parts around player

Hello, I’m trying to implement this post in my script shown below but I’m unsure on how to. It’s more than likely a simple addition but I keep coming across errors. The script shown is a pet follow script, but I want to change newPosition = character.HumanoidRootPart.CFrame * CFrame.new(0,0,10) so that the pet’s position changes depending on how many pets are equipped.

Pet follow script

local function petEquip(player,pet)

		print("PET ACTIVATED")

		local character = player.Character

		pet.Parent = character

		local petPrimaryPart = pet.PrimaryPart

		local bodyPosition = Instance.new("BodyPosition",petPrimaryPart)
		bodyPosition .P = 10000
		bodyPosition .D = 500
		bodyPosition .MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		
		local bodyGyro = Instance.new("BodyGyro", petPrimaryPart)
		bodyGyro.P = 200
		bodyGyro.D = 50
		bodyGyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		
		game:GetService("RunService").Heartbeat:Connect(function()
			newPosition = character.HumanoidRootPart.CFrame * CFrame.new(0,0,10)
			bodyPosition.Position = newPosition.p
			bodyGyro.CFrame = CFrame.new(bodyPosition.Position,character.HumanoidRootPart.Position) 
		end)
	end

I know It’s probably obvious but any suggestions will be appreciated.

Well I have a tutorial video on making pets following, it takes into account the position for 3 pets, it would require you to use a different script however

I’ve just seen your equipping script in your video. I tried a similar approach in changing the position depending on the number of pets but because the script I use utilises a loop, when the position changes all the pets go to that position.

What if you make a offset variable that gets incremented each iteration and then add/multiply the newPosition variable by it

This would work if the script didn’t control all the pets equip. Whatever changes I make to the newPosition variable replicates to all the equipped pets. I might as well use another method for the pet movement script that doesn’t require a loop but I really want to get the code mentioned if I can.

Sorry I wasn’t much help if you still have this issue, once I’m able to get to my computer (I’m on mobile) I would be able to offer more help (hopefully lol)

How about parenting the pets to a group called “Pets”? This way you can get the amount of pets the player has equipped and then offset every pet based on the total amount of pets.

Exactly what I’ve done before. But again, any changes I make to the newPosition replicates to all the pets. So if I equip two pets both pets will be offset to the second pet position.

How about:

  1. you group all Pets under the Character.

  2. have the pet handling be done by a single script which loops through the Pets group and applies the offset.

Assuming you want a circular pattern and n number of pets you can imagine dividing the circle into n many sectors and then placing a pet offset at the edge between each sector. In code:

local PET_NUM = 3 -- any int > 0
local baseRotOffset =  CFrame.Angles(0, 0, 0) -- to configure the base rotation of where one pet would be
local basePosOffset = CFrame.new(0, 0, 10) -- change the distance from the center to the pets (radius of the circle)
local arcDiff = 2 * math.pi / PET_NUM -- radian equivalent to 360 degrees/ PET_NUM
-- doesn't matter if you iterate starting at 0 or 1 but will change which pet is original offset
for i, pet in ipairs(pets) do
    -- this would be the cf offset from the player (optional to include the players rotation)
    local cfOffset = baseRotOffset * CFrame.Angles(0, arcDiff * i) * basePosOffset
end
2 Likes