Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index field 'PrimaryPart' (a nil value)?

Asked by 5 years ago

Hello! So I'm trying to make a keycard door that opens but when I touch it with my keycard it gives my the error in the title. Here is my code:

local db= false
local open = script.Parent.Parent.Open
script.Parent.Parent.Trigger.Touched:Connect(function(p)
    if db == false then
        db = true
        if p.Parent:FindFirstChild("Card") ~= nil then
            if open.Value == false then
                local f = script.Parent.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                local f2 = script.Parent.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Parent.Door1.PrimaryPart.CFrame:lerp(f,i)
                    local cfm2 = script.Parent.Parent.Door2.PrimaryPart.CFrame:lerp(f2,i)
                    script.Parent.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = true
                wait(3) -- how long does the doors open
                local f3 = script.Parent.Parent.Door1.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0)
                local f4 = script.Parent.Parent.Door2.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0)
                for i = 0,1,0.1 do
                    local cfm = script.Parent.Parent.Door1.PrimaryPart.CFrame:lerp(f3,i)
                    local cfm2 = script.Parent.Parent.Door2.PrimaryPart.CFrame:lerp(f4,i)
                    script.Parent.Parent.Door1:SetPrimaryPartCFrame(cfm)
                    script.Parent.Parent.Door2:SetPrimaryPartCFrame(cfm2)
                    wait()
                end
                open.Value = false
            end
        else
            print("User has no card")
        end
        db=false
    end
end)


0
That just means no primary part is set for your model, however you should really post where the line is occurring User#24403 69 — 5y

1 answer

Log in to vote
1
Answered by
green271 635 Moderation Voter
5 years ago
Edited 5 years ago

The Error

The error is thrown because you have tried to modify/access the PrimaryPart of the model. Since there is none, the error says that it is a nil value (no value).

Solution

The error is present in both Door1 and Door2. Make sure they are models, as PrimaryPart's are not present in Parts.

If you want to do it manually, click the empty field next to PrimaryPart, and then click the part that you want to be it's primarypart.

To do it with a script:

game.Workspace.Model.PrimaryPart = game.Workspace.Model.Part -- made up names & locations
0
the primary part needs to be inside the model so your attempt would not work, game.Workspace.Model.Part works is there is a part named Part in there User#24403 69 — 5y
0
was not aware of that, edited. thanks green271 635 — 5y
Ad

Answer this question