PrimaryPart apparently not set, even tho it is set?

local function renderPet(pet)
	--pet.CanCollide = false
	
	local targetPlr = Players[pet.Name]
	
	local bp = Instance.new("BodyPosition")
	local bg = 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 = 2000
				
	bg.Parent = pet
	bp.Parent = pet
	
-- ERROR BELOW
	pet:SetPrimaryPartCFrame(targetPlr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0))
end

-- Add pets from container
for i, v in pairs(PetContainer:GetChildren()) do
	renderPet(v)
end

-- Add new pets added to container
PetContainer.ChildAdded:Connect(renderPet)
1 Like

I had a similar problem in the past, my solution was to just set the primary part of the model manually before using SetPrimaryPartCFrame, though there may be a better solution. I’ve noticed that it seems the PrimaryPart property tends to be very finicky and so I’ve tried to stay away from it myself.

Hope this helps!

Even so, says it doesn’t exist

if not pet.PrimaryPart then
		pet.PrimaryPart = pet.HumanoidRootPart
	end
	
	pet:SetPrimaryPartCFrame(TargetCharacter.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0))

HumanoidRootPart is not a valid member of Model

have you tried using :WaitForChild?

Strange. I just booted up an empty instance of studio, replicated the explorer structure as close as possible, and got no errors. The cube pet spawns in above the character without issue. BodyGyro and BodyPosition are all present.

My current setup:

  • Button triggers RemoteEvent
  • ServerScript receives event, clones pet
  • Pet script handles everything, adds the body gyro/position and sets CFrame to above player position.

Is the pet created on the client or server?

Created, placed in a folder and SetNetworkOwner all on the server

I’m also setting NetworkOwnership of the pet to the player from the server too

-- Clone pet
	local ClonedPet = EquippedPet:Clone()
	ClonedPet.Name = player.Name
	
	-- Set position
	ClonedPet:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(2, 2, 2))
	
	-- Parent
	ClonedPet.Parent = PetContainer
	
	-- Set Network owner
	ClonedPet.PrimaryPart:SetNetworkOwner(player)

Still no errors on my part. This is what my main server script looks like:

local PetContainer = workspace.PetContainer
local Players = game:GetService("Players")
local event = game:GetService("ReplicatedStorage").DoThing

local function renderPet(pet)
	--pet.CanCollide = false
	
	local targetPlr = Players[pet.Name]
	
	local bp = Instance.new("BodyPosition")
	local bg = 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 = 2000
				
	bg.Parent = pet
	bp.Parent = pet
	
-- ERROR BELOW
	pet:SetPrimaryPartCFrame(targetPlr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0))
end

-- Add pets from container
for i, v in pairs(PetContainer:GetChildren()) do
	renderPet(v)
end

-- Add new pets added to container
PetContainer.ChildAdded:Connect(renderPet)

event.OnServerEvent:Connect(function(player)
	if(game:GetService("ServerStorage"):FindFirstChild(player.Name)) then
		local clonePet = game:GetService("ServerStorage"):FindFirstChild(player.Name):Clone()
		clonePet.Parent = workspace.PetContainer
		clonePet.PrimaryPart:SetNetworkOwner(player)
	end
end)

Try printing the PrimaryPart directly after you clone the model, and connect :GetPropertyChangedSignal(“PrimaryPart”) to see if it gets changed somehow.

I’m doing the renderPet from the client. Running that on the server would cause huge problems if there are 50 or so players in game, all with pets

I see.

Made some changes to the structure, sozzly’s method worked.

local PetContainer = workspace.PetContainer
local Players = game:GetService("Players")
local event = game:GetService("ReplicatedStorage").DoThing

local function renderPet(pet)
	--pet.CanCollide = false
	
	local targetPlr = Players[pet.Name]
	
	local bp = Instance.new("BodyPosition")
	local bg = 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 = 2000
				
	bg.Parent = pet
	bp.Parent = pet
	
-- ERROR BELOW
	print(pet:WaitForChild("HumanoidRootPart"))
	pet:SetPrimaryPartCFrame(targetPlr.Character.HumanoidRootPart.CFrame + Vector3.new(0, 10, 0))
end

-- Add pets from container
for i, v in pairs(PetContainer:GetChildren()) do
	renderPet(v)
end

-- Add new pets added to container
PetContainer.ChildAdded:Connect(renderPet)

This is a localscript located in StarterPlayerScripts. I added the print statement which seems to have fixed the issue, though there’s probably a more elegant way of implementing it.

Hmm, this seems odd. Here is my advice:
First, go on the server-side and check if a PrimaryPart is set there. (If not, make a serverscript that sets a primarypart)
If results are the same, you should contact a Devforum Staff Member. (Or you should ask other devs)

I’m not sure why, but it keeps doing this. I disabled the client script, cause I thought it was the Body stuff, but it still does it


Here’s the full server

--[[
	// NinjoOnline \\
	
	PetManager
--]]

local PetManager = {}

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local Pets = ReplicatedStorage:WaitForChild('Pets')
local Remotes = ReplicatedStorage:WaitForChild('Remotes')

local Events = Remotes:WaitForChild('Events')
local EquipPet = Events:WaitForChild('EquipPet')
local UnequipPet = Events:WaitForChild('UnequipPet')

local PetContainer = workspace:WaitForChild('PetContainer')

local function Equip(player, pet)
	-- Check for pet in folder
	local EquippedPet = Pets:FindFirstChild(pet)
	if not EquippedPet then return end
	
	--/// EDIT Check to make sure player owns pet
	
	-- Clone pet
	local ClonedPet = EquippedPet:Clone()
	ClonedPet.Name = player.Name
	
	-- Set position
	ClonedPet:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(2, 2, 2))
	
	-- Parent
	ClonedPet.Parent = PetContainer
	
	-- Set Network owner
	ClonedPet.PrimaryPart:SetNetworkOwner(player)
end

local function Unequip(player, pet)
	
end

EquipPet.OnServerEvent:Connect(Equip)
UnequipPet.OnServerEvent:Connect(Unequip)

return PetManager

Try checking every script if any of them set the model’s PrimaryPart’s value to nil,
also check if no scripts replace or delete the PrimaryPart aswell.
I had a similar problem and it was because something I scripted earlier kept changing the valut to nil.