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

Problem with my door?

Asked by
Relatch 550 Moderation Voter
9 years ago
function touch(hit)
    p = game.Players:GetPlayerFromCharacter(hit.Parent)
    if p.Backpack.Key ~= nil then
        script.Parent.CanCollide = false
        wait()
        script.Parent.Transparency = .5
    else
        print("Doesnt have key!")
    end
end

script.Parent.Touched:connect(touch)

When I touch the part with my key, it says:

Workspace.Part.Script:3: attempt to index global 'p' (a nil value)

2 answers

Log in to vote
0
Answered by 9 years ago

here I have an idea for you to identify the character

function touch(hit)
    if not hit.Parent:IsA("Model") then
        Character = hit.Parent.Parent -- because it might be a tool or a hat
    else
        Character = hit.Parent -- then its a character because the character is a model
    else
        return nil -- if its neither
    end
    local p = game.Players:GetPlayerFromCharacter(Character) -- gets player from the variable Character
    if p.Backpack.Key ~= nil then
           script.Parent.CanCollide = false
            wait()
            script.Parent.Transparency = .5
    else
            print("Doesnt have key!")
    end
end
script.Parent.Touched:connect(touch)

please vote up if this helped

Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Since the key you're touching is the part, the part's parent is the tool. Maybe redirect the part to the tool to the character itself (hit.Parent.Parent). Or follow what coltn5000 demonstrated.

Answer this question