Error with position

I have problem with position. the error says

attempt to index nil with 'Position

this is my script

if (player.Character.HumanoidRootPart.Position - v.PrimaryPart.Position).Magnitude < 5 then

any help is appriciated :smiley:

Can I see the entire script, it’s kindah ard to judge from only one line.

sorry late…
so as requested:

local player = game:GetService("Players")
local localplayer = player.LocalPlayer
local Character = localplayer.Character or player.CharacterAdded:Wait()
local bill = script.Parent.BillboardGui
local but = bill.TextButton

local doorparent = game.Workspace.Door

local UseInputService = game:GetService("UserInputService")

UseInputService.InputBegan:Connect(function(input, gameproces)
	for i , v in pairs(doorparent:GetChildren())do
		if (Character.HumanoidRootPart.Position - v.PrimaryPart.Position).Magnitude < 5 then
			bill.Adornee = v
			print("nearby passenger")
			if not gameproces then
				if input.KeyCode == Enum.KeyCode.E then
				if bill.Adornee == v then	

                                -- do stuff--

dont mind my messy code

What are the children of doorparent?
If you’ve got anything in there that’s either not a model, or that is a model without primary part set, that could be the problem

v is model
v has primarypart

OK, just checking because I’d guess (with your earlier checks for waiting for character to load) that it’s not the humanoid.

But, easiest way to check is add some prints to debug it; put in

	for i , v in pairs(doorparent:GetChildren())do
		if (Character.HumanoidRootPart.Position - v.PrimaryPart.Position).Magnitude < 5 then

Then look in the output. One of these somewhere will be nil, when you know which will help to track down the problem.
As you’ve waited for the character to load I’ll still guess it must be one of the different v’s that you are iterating through, but debugging would show if its that or not

(or put a breakpoint on and just check the values of things when it gets hit, either works)


this part saying nil. but I dont understand

What did the prints before that show, so before that printed nil what did print(v) output and what did print(v.PrimaryPart) output?

it did print except the last part

print(v) – print the name of the model

print(v.PrimaryPart.Position) – print nill

what did print(v.PrimaryPart) print out?

oops…silly me

print(v.PrimaryPart) – print nill

(v.PrimaryPart.Position) --print nothing

but when I clicked the error it show the (v.PrimaryPart.Position)

Yea I think that’s where it gives an error, cos you are trying to access .Position of something that’s nil there. And that’s what will break.
So should just need to set the primary part on the model and it’s all good.

1 Like

but the model already has the primary part .I checked all of them

Hmm it can’t do if it says its nil here. I’m guessing there’s more than one model inside of game.Workspace.Door, as you’re looping through them

The name printed out by print(v) might help you to narrow down which one doesn’t have a primary part
So look in the out put for

print(v) → The model name
print(v.PrimaryPart) > nil

Then find the model with the name that was printed out just before this nil, and it should be that’s missing a primary part.

(I’m assuming here there’s no other scripts or anything running that would be changing these models or removing their primary part in any way)

it print this one name


I already checked this model but there are nothing sus

Hmmm, so it prints out

print(v) > DoorIdk
print(v.PrimaryPart) > nil
print(v.PrimaryPart.Position) > errors here

And if you check that it’s all set up OK, so looks like

If that’s all ok before you press run, then also run your game. While it’s running, just after the error has happened, look in explorer and find the DoorIDK model again, is it still looking OK then? Does it still have its primary part?

When I click play…everything ok nothing seems changing only got an error and thats weird

So should I change Position to Origin Position instead??

This means that whatever you are taking the position of is nil. This meaning that the code cannot find either the PrimaryPart or the HumanoidRootPart that you are referencing.

1 Like