Problem with pet following script

Hello there, I know this issue have been solved many times but I can’t seem to figure out the problem. The bodyposition and bodygyro doesnt work. This is a local script inside StarterCharacterScripts and I parent the pets to EquippedPets folder from the server.

Here’s the script :

local PetsEquipped = script.Parent:WaitForChild("EquippedPets")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local humRootPart = Character.HumanoidRootPart

local function GetPointOnCircle(CircleRadius, Degrees)
	return Vector3.new(math.cos(math.rad(Degrees)) * CircleRadius, 0, math.sin(math.rad(Degrees)) * CircleRadius)
end

local function petFollow(pet, Pos, bp, bg)
	spawn(function()
		print(pet.PrimaryPart.Size)
		RunService.RenderStepped:Connect(function()
			bp.Position = (humRootPart.CFrame * CFrame.new(Pos)).p - Vector3.new(0, humRootPart.Size.Y / 2, 0) + Vector3.new(0, pet.PrimaryPart.Size.Y, 0)
			bg.CFrame = humRootPart.CFrame
		end)
	end)
end

PetsEquipped.ChildAdded:Connect(function(child)
	print(child.Name)
	local bp, bg = Instance.new("BodyPosition"), Instance.new("BodyGyro")
	
	bg.D = 750
	bg.MaxTorque = Vector3.new(25000, 25000, 25000)
	bg.P = 5000
		
	bp.D = 400
	bp.MaxForce = Vector3.new(1250, 10000, 1250)
	bp.P = 10000
	
	bp.Parent, bg.Parent = child.PrimaryPart, child.PrimaryPart
	
	print(child.Parent)
	petFollow(child, GetPointOnCircle(5, 360/#PetsEquipped:GetChildren()), bp, bg)
end)

Everything prints out but after some time, the 15th line errors (because the pet parent is nil)
bp.Position = (humRootPart.CFrame * CFrame.new(Pos)).p - Vector3.new(0, humRootPart.Size.Y / 2, 0) + Vector3.new(0, pet.PrimaryPart.Size.Y, 0)
Workspace.Epic_Player16.Pets:15: attempt to index nil with ‘Size’

Hope you can help.

Oh, no, the problem is that pet parent is set to nil after some time because everything inside the pet is unanchored, but I cant anchor it or else bodyposition wont work.

is the pet’s primary part a model? are you sure that humRootPart is returning as the characters humanoid root part?

No. It’s a basepart.

Pretty sure.

Why exactly is the parent of the pet becoming nil? Is it falling out of the map? Are you sure pet.PrimaryPart is equal to a instance with a Size property to begin with?

why don’t you print out the “humrootpart”, then print out “humrootpart.Size”?

1 Like

In addition to this, print out pet.PrimaryPart.Size, to make sure it isn’t returning nil. Also, if you just print “pet”, does it print nil @Epic_Player16?

I print pet.PrimaryPart and it says nil. However, I have set a primarypart for every of my pet.
Maybe I should parent the pet from the client and not server?

heh, there’s our issue…

  1. make sure that the pet has a valid primary part that isn’t getting cloned / deleted etc.
  2. try manually setting the pets primary part via script.
  3. do a for i,v in pairs(pet:GetChildren()) do loop, and then print out v

I’m printing pet.PrimaryPart.Size and it does print out? Its weird, when I do print(pet.PrimaryPart), its nil but size prints out.

then the primary part isn’t nil?..
also that does not address what i suggested to do.
however this makes me think you may be making a typo, or some variable name mess up?..

Hmm,

Prints nothing, there’s no child in the pet? It’s a really weird bug huh.

bp.Parent, bg.Parent = child.PrimaryPart, child.PrimaryPart
This works, doesnt error. PrimaryPart isnt nil but when I print it, it prints nil…

for i,v in pairs(pet:GetChildren()) do
    print(v)
end)

not understanding why i needed to show this, but yeah that was an example.
i’m thinking this is something super simple that we / i can’t spot

By any chance are you parenting anything else, other than a pet? Since you are using ChildAdded with no checks, the event could fire when something other than a pet is added.

Try printing “child”, to make sure it is a pet, and not something else.

I did it, it prints nothing. (30charrs)

Printed the names, its only pets names.

are you sure you’re typing everything correctly, passing function parameters correctly etc?
if the loop printed nothing, do something like this

print(v.Parent)

also where are you putting the for i,v in pairs loop?

directly when the childevent is fired?

PetsEquipped.ChildAdded:Connect(function(child)
	for i,v in pairs(child:GetChildren()) do
		print(v, v.Parent)
	end
	local bp, bg = Instance.new("BodyPosition"), Instance.new("BodyGyro")
	
	bg.D = 750
	bg.MaxTorque = Vector3.new(25000, 25000, 25000)
	bg.P = 5000
		
	bp.D = 400
	bp.MaxForce = Vector3.new(1250, 10000, 1250)
	bp.P = 10000
	
	bp.Parent, bg.Parent = child.PrimaryPart, child.PrimaryPart
	
	petFollow(child, GetPointOnCircle(5, 360/#PetsEquipped:GetChildren()), bp, bg)
end)
``` Prints absolutely nothing.