Help Pet Following Like in Pet Simulator X

How to make pets follow players back every time, just like in Pet Simulator X.

My System:

Pet Simulator X:

Script

local PetMovementHandler = {}

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local PlayerPets = workspace:WaitForChild("PlayerPets")

local HalfCircle = math.pi


local function getXAndZPositions(angle, scalar)
	local x = math.cos(angle) * scalar
	local z = math.sin(angle) * scalar
	return x, z
end

function getAngle(touch, center)
	return math.atan2(touch.X - center.X, touch.Z - center.Z)
end


local function UpdatePetPosition()

	for _, Folder in pairs(PlayerPets:GetChildren()) do
		local Player = Players[Folder.Name]
		local Character = Player.Character


		if Character and Character.PrimaryPart then
			local HumanoidRootPart = Character.HumanoidRootPart
			local PetTable = PlayerPets:WaitForChild(Player.Name):GetChildren()

			local XCos = math.cos(5 * time() + 1)/4
			local ZCos = math.cos(7 * time() + 1)/4
			local YSin = math.sin(5 * time() + 1.6)/2
			local YSin2 = math.sin(15 * time() + 1.6)/.5
			

			for Index, Pet in ipairs(PetTable) do

				local i = Index

				local pets_per_row = 5
				local base_radius = 4
				local radius_per_row = 3
				local row = math.floor((i - 1) / pets_per_row)
				local radius = row * radius_per_row / 1.5 + base_radius

				local angle = ((i - 1) % pets_per_row) * (HalfCircle / pets_per_row)
			

				local angle = ((i - 1) % pets_per_row) * (HalfCircle / pets_per_row)
				local X, Z = getXAndZPositions(angle, radius) 
				
				local X, Z = getXAndZPositions(angle, radius) 
				local MainYAngle = Character.Humanoid.MoveDirection.Magnitude > 0 and
					getAngle(Pet.PrimaryPart.Position, Pet.PrimaryPart.Position + HumanoidRootPart.CFrame.LookVector) or
					getAngle(Pet.PrimaryPart.Position, HumanoidRootPart.Position) 
				if Pet.CanFly.Value then
					Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(CFrame.new(HumanoidRootPart.CFrame.p) * CFrame.Angles(0, MainYAngle,0) * CFrame.fromEulerAnglesXYZ(XCos, 0, 0) + Vector3.new(X, YSin, Z), 0.1)
				else
					--local FloorYAngle = Character.Humanoid.MoveDirection.Magnitude > 0 and -Character.RightUpperLeg.Position.Y + 1 + YSin2 or -Character.RightUpperLeg.Position.Y + .05
					local FloorYAngle = Character.Humanoid.MoveDirection.Magnitude > 0 and Character.HumanoidRootPart.Size.Y/2 + -2 + YSin2 or Character.HumanoidRootPart.Size.Y/2 + -3.2
					
					if Character.Humanoid.MoveDirection.Magnitude > 0 then
						
						Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(CFrame.new(HumanoidRootPart.CFrame.Position) 
							* CFrame.Angles(0, MainYAngle,0) 
							* CFrame.fromEulerAnglesXYZ(0, 0, ZCos) 
							+ Vector3.new(X ,FloorYAngle, Z),
							0.1
						)
					else
						Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(CFrame.new(HumanoidRootPart.CFrame.Position) 
							* CFrame.Angles(0, MainYAngle,0) 
							* CFrame.fromEulerAnglesXYZ(0, 0, 0) 
							+ Vector3.new(X,FloorYAngle, Z),
							0.1
						)
					end
				end
			end
		end
	end
end

function PetMovementHandler:Int()
	RunService.Heartbeat:Connect(UpdatePetPosition)
end

return PetMovementHandler
2 Likes

Use bodyvelocity, and adjust the velocity of the pets based off how far they are from the target goal (so they slow down behind the player to make it look smooth if the player stops). Goal behind the player being some magnitude of negative hrp lookvector

1 Like

Is there no other method without using body velocity?

1 Like

I don’t understand the system you are using now but it looks like pet simulator x has the pets constantly locked a certain distance behind the player. You just need to find a way to use the negative lookvector of the hrp so that the pets will always be a certain distance behind the player, whether that’s possible with the current maths you are using idk

I found a way, you’d have to do a lot of editing but the principle is the same. Just tween the pets behind the player. This is just with a part and top script works just for my character because I’m lazy.

–Demo script–

wait(3) --cba so character loads
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")
local v3n,cfn = Vector3.new,CFrame.new
local hrp = workspace.Azarctic.HumanoidRootPart
local hrpcf = hrp.CFrame

local pet = Instance.new("Part",workspace)
pet.Anchored = true

RS.Heartbeat:Connect(function()
	local hrpx,pety,hrpz = hrp.Position.X,pet.Position.Y,hrp.Position.Z
	local hrplv = hrp.CFrame.LookVector
	local base = v3n(hrpx,pety,hrpz)
	local goalcf = cfn(base - 10*hrplv, base)

	TS:Create(pet, TweenInfo.new(2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out), {CFrame = goalcf}):Play()	
end)
Pet.PrimaryPart.CFrame = Pet.PrimaryPart.CFrame:Lerp(HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(X ,FloorYAngle, Z)) 
* CFrame.Angles(0, MainYAngle,0) 
* CFrame.fromEulerAnglesXYZ(0, 0, ZCos),
0.1)

the video isn’t working can you please fix it