CFrame with PrimaryPart problem

I am trying to rotate a model inside of a viewport. I am getting the error that the PrimaryPart doesn’t exist or has been changed which it hasn’t at all. I tried seeing if me changing the parent of the model ruined but that was not the case. The following code is here:

local runService = game:GetService("RunService") 
local startingTick = tick()

local vf = script.Parent

local cf = CFrame.new(Vector3.new(0, 0, 0))
script.Rifle.Parent = vf
script.Parent.Rifle:SetPrimaryPartCFrame(cf)


local camera = Instance.new("Camera", vf)
vf.CurrentCamera = camera

local cameraPosition = Vector3.new(3, 0, 2)
camera.CFrame = CFrame.new(cameraPosition, script.Parent.Rifle:GetPrimaryPartCFrame())

function makeBrickMove()
	while true do 
		local diff = tick() - startingTick 
		script.Parent.Rifle:SetPrimaryPartCFrame(CFrame.Angles(0, 10*math.rad(diff), 0)) 
		runService.RenderStepped:Wait()
	end
end

makeBrickMove()

Error:

  Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.  -  Client - LocalScript:8

The Model:
Screenshot 2021-05-15 115316

Thanks if you could help

change this to this

local cf = CFrame.new(Vector3.new(0, 0, 0))

to

local cf = CFrame.new(0, 0, 0)

Thanks for helping me simplify this, but this does not help my error.

could you show the explorer?b

image

how does the model property look when test playing game?

this line feels weird

camera.CFrame = CFrame.new(cameraPosition, script.Parent.Rifle:GetPrimaryPartCFrame())

The model looks like this:

You should check if the PrimaryPart exists first.

if Model.PrimaryPart then
   -- Code.
end

So actually I have found out that since I changed the parent of the model it reset the PrimaryPart to nil for some reason. I fixed it with this simple line of code:

model.PrimaryPart = part

I appreciate the help though :slight_smile: