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’
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.
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?
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?
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?..
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.