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

Output keeps erroring even though If statement deals with that condition?

Asked by 6 years ago

I have the following code:

local door = script.Parent
local allowed = {"boogieboon", "someothername"}

door.touched:connect(function(hit)
    local character = hit.Parent
    if character.Humanoid then
        for i,v in pairs(allowed) do
            if character.Name == v then
                door.Transparency = 1
                door.CanCollide = false
                wait(1)
                door.Transparency = 0
                door.CanCollide = true
            end 
        end  
    end 
end)

I was just wondering why the output keeps returning "Humanoid is not a valid member of Accessory" for line 6 even though I have an if statement that is supposed to deal with what to do if it doesn't have a humanoid in it? It's not a game-breaking problem but I would just like to know how to prevent the output from erroring since I'm trying to deal with this error in the if statement. Thanks, Any help is appreciated.

0
when you index a child that is nil it errors, so use FindFirstChild in the if statement LaeMVP 0 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

Your accessories are hitting the door instead of your body parts. Accessories are "set up" so that there's the accessory container and then a handle inside of it that actually comes in contact with other objects.

You need to add a debug piece so that if the character is nil you search for hit.Parent.Parent.

if humanoid == nil then
    humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end

^ something like that.

Ad
Log in to vote
0
Answered by
GingeyLol 338 Moderation Voter
6 years ago
Edited 6 years ago
local door = script.Parent
local allowed = {"boogieboon", "someothername"}

door.Touched:Connect(function(hit)
    local character = hit.Parent
    if character.Humanoid then
        for i,v in pairs(allowed) do
            if character.Name == v then
                door.Transparency = 1
                door.CanCollide = false
                wait(1)
                door.Transparency = 0
                door.CanCollide = true
            end 
        end  
    end 
end)

Answer this question