Is the PrimaryPart not set immediately?

Every hour or so, my error logger collects an error like this:

[06/13/2018 - 15:27.20] Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.

All of the models with primary parts that I access are preset in Studio, none of them are set dynamically. These errors sometimes pop up when one of these models is cloned and its primary part is immediately accessed.

If I know that the PrimaryPart exists, do I have to add something like this as a failsafe in the case that the PrimaryPart isn’t set immediately after the model is cloned?

if not Model.PrimaryPart then
   Model:GetPropertyChangedSignal("PrimaryPart"):Wait()
end

Thanks

1 Like

Could it be that you are using :Destroy() on models with PrimaryParts?
I wrote a sample script that would cause this to happen:

local model = workspace:WaitForChild("Model")
local pos = model.PrimaryPart.CFrame
local yAngle = 0

game:GetService("RunService").RenderStepped:connect(function()
	model:SetPrimaryPartCFrame(pos*CFrame.Angles(0,math.rad(yAngle),0))
	yAngle = yAngle + 1
	if yAngle > 359 then
		yAngle = 0
	end
end)
wait(2)
model:Destroy()

~~
IF you were getting this error then it would be a problem of having a primarypart in the first place, which is not the case for you:
14:35:20.639 - Workspace.Script:2: attempt to index field ‘PrimaryPart’ (a nil value)
~~
Either way, if you want this to not happen, with the example script above, I would disconnect the renderstepped before destroying the model like so

Fix
local model = workspace:WaitForChild("Model")
local pos = model.PrimaryPart.CFrame
local yAngle = 0

local connection = game:GetService("RunService").RenderStepped:connect(function()
	model:SetPrimaryPartCFrame(pos*CFrame.Angles(0,math.rad(yAngle),0))
	yAngle = yAngle + 1
	if yAngle > 359 then
		yAngle = 0
	end
end)
wait(2)
connection:Disconnect()
model:Destroy()

Notice the connection variable

3 Likes

Turns out the issue was similar to this. I upgraded my error logging to include the stack trace, and with that I was finally able to pinpoint the exact error location.

The issue came from a loop that changed the Character’s PrimaryPartCFrame for a short period of time. Seems like the error would appear whenever somebody reset/left during the loop.

Thanks for the help!

1 Like