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

Why doesn't my else statement run when my if statement isn't fullfilled?

local Door = script.Parent

function onTouch(Brick)
	local Player = Brick.Parent:findFirstChild("Humanoid")
	if(Player ~= nil) then
		local TShirt = Player.Parent:findFirstChild("Shirt Graphic")
		if TShirt.Graphic == "http://www.roblox.com/asset/?version=1&id=1028594" then
			Door.CanCollide = false
			Door.Transparency = 0.5
			Door.BrickColor = BrickColor:Random()
			wait(2)
			Door.CanCollide = true
			Door.Transparency = 0
			Door.BrickColor = BrickColor:Random()
		else Player.Health = 0
		end
	end
end

Door.Touched:Connect(onTouch)

So my code checks if the player has this exact t shirt and if the player has it will make the player able to pass through a door. This works. However when a player doesnt have the exact t shirt it is meant to kill the player. This doesnt work. So when i walk into the door without the t shirt it just says "Workspace.Part.Script:7: attempt to index nil with 'Graphic'" can anyone help with making my else statement run?

2 answers

✓ Accepted answer
if TShirt and TShirt.Graphic == "http://www.roblox.com/asset/?version=1&id=1028594" then

2 comments

0

it works now but why does it work now what is the difference that i add and "and"?

0

Because when there is no tshirt it will error and when you check if theres a tshirt it will not error and run else

else Player.Humanoid.Health = 0

1 comment

0

wrong because player is the humanoid in this script. And the problem isn't that but that the else doesn't even run

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