Attempt to index nil with position

  1. What do you want to achieve? I am making a keybind door
  2. What is the issue?
RunService.RenderStepped:Connect(function()
    if Player:DistanceFromCharacter(DoorPrimaryPart.Position) < 5 then
        NearDoor = true
        DoorPrimaryPart.EToOpen.Enabled = true
    else
        NearDoor = false
        DoorPrimaryPart.EToOpen.Enabled = false
    end
end)
 

This is my code the error says: attempt to index nil with ‘Position’

  1. What solutions have you tried so far? I looked on google and on the dev forum and didn’t find answers
5 Likes

is DoorPrimaryPart defined? Did you mean to make it “Door.PrimaryPart.Position” instead?

Yes, the DoorPrimaryPart is defined

1 Like

Can I see the whole script instead of this snippet?

local minimum = 3
local groupID = 6792809
local authorized = false

local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
 
 
local Door = game.Workspace.Kitchen_Door
local DoorPart = Door.Door
local DoorPrimaryPart = Door.PrimaryPart
 
local NearDoor = false
 
if game.Players.LocalPlayer:GetRankInGroup(groupID) >= minimum then
	authorized = true
end

RunService.RenderStepped:Connect(function()
    if Player:DistanceFromCharacter(DoorPrimaryPart.Position) < 5 then
        NearDoor = true
        DoorPrimaryPart.EToOpen.Enabled = true
    else
        NearDoor = false
        DoorPrimaryPart.EToOpen.Enabled = false
    end
end)
 
UserInputService.InputBegan:Connect(function(Input)
    if Input.UserInputType ==   Enum.UserInputType.Keyboard then
        local Key = Input.KeyCode
        if Key == Enum.KeyCode.E and NearDoor == true and authorized == true then
         	 Door.Beep:Play()
			DoorPart.CanCollide = false
			wait(5)
			DoorPart.CanCollide = true
        end
    end
end)

3 Likes

Try this:

local minimum = 3
local groupID = 6792809
local authorized = false

local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
 
 
local Door = game.Workspace.Kitchen_Door
local DoorPart = Door.Door
local DoorPrimaryPart = Door.PrimaryPart
 
local NearDoor = false
 
if game.Players.LocalPlayer:GetRankInGroup(groupID) >= minimum then
	authorized = true
  else
    return
end

RunService.RenderStepped:Connect(function()
    if Player:DistanceFromCharacter(DoorPrimaryPart.Position) < 5 then
        NearDoor = true
        DoorPrimaryPart.EToOpen.Enabled = true
    else
        NearDoor = false
        DoorPrimaryPart.EToOpen.Enabled = false
    end
end)
 
UserInputService.InputBegan:Connect(function(Input)
    if Input.UserInputType ==   Enum.UserInputType.Keyboard then
        local Key = Input.KeyCode
        if Key == Enum.KeyCode.E and NearDoor == true and authorized == true then
         	 Door.Beep:Play()
			DoorPart.CanCollide = false
			wait(5)
			DoorPart.CanCollide = true
        end
    end
end)
1 Like

That’s because DoorPrimaryPart is nil, consider defining it somewhere.

It does not work, the error is on this line

but thanks for trying

2 Likes

You shouldn’t just give out scripts straight away like that. Instead, try to explain it and go through what is going around the script.

1 Like

Try to use Magnitude instead.
Try this:

local minimum = 3
local groupID = 6792809
local authorized = false

local Player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
 
 
local Door = game.Workspace.Kitchen_Door
local DoorPart = Door.Door
local DoorPrimaryPart = Door.PrimaryPart
 
local NearDoor = false
 
if game.Players.LocalPlayer:GetRankInGroup(groupID) >= minimum then
	authorized = true
  else
    return
end

RunService.RenderStepped:Connect(function()
    if (Player.Character.Head.Position - Door.Position).Magnitude < 5 then
        NearDoor = true
        DoorPrimaryPart.EToOpen.Enabled = true
    else
        NearDoor = false
        DoorPrimaryPart.EToOpen.Enabled = false
    end
end)
 
UserInputService.InputBegan:Connect(function(Input)
    if Input.UserInputType ==   Enum.UserInputType.Keyboard then
        local Key = Input.KeyCode
        if Key == Enum.KeyCode.E and NearDoor == true and authorized == true then
         	 Door.Beep:Play()
			DoorPart.CanCollide = false
			wait(5)
			DoorPart.CanCollide = true
        end
    end
end)
4 Likes

The problem here, is that your door’s primary part is nil, you didn’t set it. Go to your door model and set the primary part, then it won’t be nil and that should work. The primary part is in the door’s properties.
image

Also, make sure that this is the right path to the door, because if it isn’t, then the door is nil too, and that’s why the primary part is not detected (if it exists)

4 Likes

well he can do what he wants i guess.

1 Like