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

Why is "player" a nil value?

Asked by 5 years ago

There is a Workspace.Part.Script:5: attempt to index local 'player' (a nil value) poping up in the output - can anyone explain it so i can understand - goku

local part = script.Parent

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player:IsInGroup(2793019) then
        print("eeeeeeeeeeeeeeeee")
    end

end)

0
GetPlayerFromCharacter will return nil if it cannot get the player from the character model. User#5423 17 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

You are not checking if what Touched is even Part of a Character. To do so check if hit.Parent has a HumanoidRootPart.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Try this;

local part = script.Parent

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent or hit.Parent.Parent)
    if player and player:IsInGroup(2793019) then
        print("eeeeeeeeeeeeeeeee")
    end
end)

I believe it's because the part is touching an accessory or some other child of a part within the Character player model. If that's hard to understand let me know, I'm not good at explaining.

0
This won't work because of how or works. Or only chooses the second one if the first one is nil, and if there's a hit.Parent.Parent of course hit.Parent isn't nil. User#22604 1 — 5y

Answer this question