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

What am I doing wrong?

Asked by 8 years ago

script.Parent.Touched:connect(function(hit)

local humanoid = hit.Parent:FindFirstChild("Humanoid")

local p = game.Players:GetPlayerFromCharacter(humanoid.Parent) p.PlayerGui.Inv.OpenInventory.Inventory.Boarder.SlotOne.Image = ""

end)

This is suppose to make the Image dissapear, But doesn't work giving me this error.

Workspace.Speed.Script:5: attempt to index local 'humanoid' (a nil value)

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

FindFirstChild either returns the child you're looking for, or it returns nil. If anything other than a Character touches this brick, then it will return nil, because hit.Parent would obviously not have a humanoid. This is your error. FindFirstChild is returning nil, then you are trying to essentially write

nil.Parent

which results in the error.

All you need to do is check if it's nil or not, using an if statement:

local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
    --etc.
end

Or, equivalently:

local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
    --etc.
end
Ad

Answer this question