Attempt to index nil with 'Position' error

Hello, so i’m currently trying to show some objects on a ViewportFrame but the output keep on saying: attempt to index nil with ‘Position’ here is my script:

local pet = script.Parent.Parent:FindFirstChildOfClass("Model")
local viewportframe = script.Parent
local cam = Instance.new("Camera")
cam.CFrame = CFrame.new(pet.PrimaryPart.Position + (pet.PrimaryPart.CFrame.lookVector * 4) + Vector3.new(1,1,1) ,pet.PrimaryPart.Position)
cam.Parent = viewportframe
viewportframe.CurrentCamera = cam

Thank you!

2 Likes

I think the problem is that the pet maybe don’t have a primary part?

Try adding a :WaitForChild() to see if it just loads slowly, but if it says “Infinite yeild” then it has no primarypart at all.

2 Likes

Do you mean something like this?

local pet = script.Parent.Parent:WaitForChild("Pet1")
local camera = Instance.new("Camera")	
camera.CFrame = CFrame.new(pet.PrimaryPart.Position + (pet.PrimaryPart.CFrame.lookVector * 4),pet.PrimaryPart.Position)
camera.Parent = script.Parent```

i did but it still saying Attempt to index nil with ‘Position’
1 Like

No, i meant like this:

local Primary = pet:WaitForChild("PrimaryPartName")
camera.CFrame = CFrame.new(Primary.Position + (Primary.CFrame.lookVector * 4),Primary.Position)

Replace PrimaryPartName with the name of the primary part. As you cant just do :WaitForChild(“PrimaryPart”)

2 Likes

i tried now the output don’t say anything but it still not displaying the object on the viewportFrame

1 Like

Are you sure it doesn’t say “Infinite yeild” or anything like that?

In whatever case it means that you have no primarypart for the pet

2 Likes

yeah i’m sure that’s so weird (30 characters)

1 Like

Have you checked if the pet even has a primarypart in studio?

1 Like

yeah i did it has a primarypart

1 Like

Where is the pet stored?
If it is in workspace it might have fallen down

2 Likes

the pet is stored inside of the button

1 Like

can you send the structure of the pet?

2 Likes

its only a model that has a part inside of it

1 Like

When doing :FindFirstChildOfClass(“Model”) it might give you something else, try just doing :FindFirstChild(“Pet Name”)

2 Likes

i even tried local pet = game.ReplicatedStorage:WaitForChild("Pet1).pet

1 Like

Didn’t you earlier say

the pet is stored inside of the button

Is the button and script in replicated storage? scripts don’t run there

1 Like

yeah but then i tried storing it on replicatedstorage

Oh.

The only problem i can see here is that you actually do not have a primarypart of the pet. I see no other reason to why it errors

1 Like

the pet do have a primarypart:
Capture

There is maybe another script that removes it? That’s the only cause i can think of.

I gtg now ill come back in a few hours.

1 Like