Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

1 comment

0

when you index a child that is nil it errors, so use FindFirstChild in the if statement

2 answers

✓ Accepted answer

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.