How can I add a code that would make more than 1 pet follow me? like 2, 3, 4, or more pets follow! SOLVED!

What code do I put with this script to add multiple pets to follow me if there are more than one pet in the “playerPets” folder?

My script:

local runService = game:GetService("RunService")

local playerPets = workspace:WaitForChild("PlayerPets")

runService.RenderStepped:Connect(function(deltaTime)
	local petOffset = workspace.CF.Value
	
	for _, folder in playerPets:GetChildren()do
		for _, pet in folder:GetChildren()do
			local playerName = folder.Name
			local character = workspace:FindFirstChild(playerName)
			
			if character == nil or not character:FindFirstChild("HumanoidRootPart") then continue end
			
			local GoalCframe = character.HumanoidRootPart.CFrame * petOffset
			
			pet:PivotTo(pet.PrimaryPart.CFrame:Lerp(GoalCframe, 0.1))
		end
	end
end)

for _, pet in folder:GetChildren() do
With that line I suppose you are already iterating in a folder that contains all pets, right?

Then you are creating the goal CFrame offset and move the pet with this:

local GoalCframe = character.HumanoidRootPart.CFrame * petOffset
pet:PivotTo(pet.PrimaryPart.CFrame:Lerp(GoalCframe, 0.1))

If you have multiple pets in that player’s folder, you are moving all those pets to the same goal CFrame, and you want to offset each pet from the others, right?
Cause your code is already moving all the pets in the folder, just to the same CFrame

Yes I want all pets to use a difference CFrame so they are not in the same place, and also I want them to never touch each other or get closer to player when player turns or rotates.

A quick way would be, grab that GoalCFrame and apply a new offset for each new pet. I mean you already have the position of the first one, then apply a small offset to that CFrame to place the second, then grab the second one CFrame and add the offset for the third pet.

What I did to make something similar its more complicated, I used Pi to make the pets surround the character in a half-shape semiCircle behind the player, so the pets are behind the player and arranged in a semiCircular shape.

Im not good at math… so I had to search in devForum on how to achieve it and found a formula which is very basic but its helpful.

This is an example of how I implemented a function that returns a table of coordinates in a halfCircle depending on how many “pets” to order and the desired radius:

function createHalfCircle(centerX, centerY, radius)
	local points = {}
	local angleStep = math.pi / 5

	for i = 1, 5 do
		local angle = math.pi + (i-1) * angleStep
		local x = centerX + radius * math.cos(angle)
		local y = centerY + radius * math.sin(angle)
		points[i] = {X = tonumber(x), Y = tonumber(y)}
	end

	return points
end

local SemiCircle = createHalfCircle(0.5, 0.5, 0.5)

Can you find the pages you used on the devForum on how you were able to achieve this? So I can see how I can add some codes to my script? Because I don’t really understand how I can apply a new offset for each new pet.

To make arrange them in a circular or semiCircular shape? Probably I checked this one and others:


To just add an offset with is way easier, its just creating a variable that contains the GoalCFrame and on each iteration of the pets, add the offset, thats pretty simple, but could look kinda “cheap”

Like:
This is just an example, cause Im grabbing the local petOffset you had in a value, and thats not the right one you want to use. Thats the offset between player and the first Pet I guess, and you need a second offset, from pet to pet

local runService = game:GetService("RunService")

local playerPets = workspace:WaitForChild("PlayerPets")

runService.RenderStepped:Connect(function(deltaTime)
	local petOffset = workspace.CF.Value

	for _, folder in playerPets:GetChildren()do
		local playerName = folder.Name
		local character = workspace:FindFirstChild(playerName)
		local GoalCframe = character.HumanoidRootPart.CFrame
		
		for _, pet in folder:GetChildren()do
			if character == nil or not character:FindFirstChild("HumanoidRootPart") then continue end
			GoalCframe = GoalCframe * petOffset
			pet:PivotTo(pet.PrimaryPart.CFrame:Lerp(GoalCframe, 0.1))
		end
	end
end)

I dont know how your first pet location looks like, maybe right behind the player? So that code would align all other pets one behind the other not on sides

You can try to implement the following code with the solution in this topic to your script

1 Like

wait, sorry, but are we talking about making the pets go in a semi circle/circle shape? if so then my solution is invalid

1 Like

Nope, you are right, I just suggested a way I like to do it, in a semi circle shape

1 Like

Your way is perfect, but now I just have to figure out how to add your code to my script without having the “Y” button to be pressed and only the pets will function with the rest of my code.

you can just set the current pets table to your playerPets:GetChildren()

Can you please retype my code with yours so I can see what you mean by it, and maybe make sure it works for you? I made it so when the pets are in the players folder in workspace the pets will start following you.

With the code that Pug64 used in that post you can easily adapt it, try this, replace all the code you did show in your post with this:

local runService = game:GetService("RunService")
local playerPets = workspace:WaitForChild("PlayerPets")

runService.RenderStepped:Connect(function(deltaTime)
	for _, folder in playerPets:GetChildren() do
		local CurrentPets = folder:GetChildren()
		local playerName = folder.Name
		local character = workspace:FindFirstChild(playerName)

		--local Deg = (math.pi*2)/#CurrentPets
		local SubI = 1
		for i,v in pairs(CurrentPets) do
			if SubI > 3 then
				SubI = 1
			end
			local Row = math.ceil(i/3)
			local CF = character.PrimaryPart.CFrame * CFrame.new(-6 + (3*SubI),0,(3*Row)+3)
			v.CFrame = CF
			SubI += 1
		end
	end
end)

Thank you it worked! I appreciate the help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.