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

why doesnt my script go to the else part if the if part is nil or false?

local function checkifitexists(whodidit)
	print(whodidit)
	if whodidit.Parent.Humanoid then
		if game.Workspace.TheMadPart then
	script.Parent.BrickColor = BrickColor.new("Lime green")
	print("The Mad Part exists!")
else
	script.Parent.BrickColor = BrickColor.new("Really red")
	print("The Mad Part doesnt exist!")
		end
		end
	end
	
	script.Parent.Touched:Connect(checkifitexists)	
	

Okay, so what I wanted to do with this script is that I have a button beside it that spawns a part called the mad part. That works but when I don't click on the button to spawn mad part and i go on the brick to check it doesn't do the else part. It just says that there is no part called "TheMadPart" I thought that it would skip to the else part if it isn't true or doesn't exist but I guess not.

2 answers

If you're in a situation where a part may or may not exist you'll want to use :findFirstChild() otherwise you'll get an error if the part can't be found. So instead of using game.Workspace.TheMadPart use game.Workspace:FindFirstChild("TheMadPart")

Try

local function checkifitexists(whodidit)
    print(whodidit)
    if whodidit.Parent.Humanoid and game.Workspace.TheMadPart then
    	script.Parent.BrickColor = BrickColor.new("Lime green")
    	print("The Mad Part exists!")
else
    script.Parent.BrickColor = BrickColor.new("Really red")
    print("The Mad Part doesnt exist!")
        end
        end
    end

    script.Parent.Touched:Connect(checkifitexists)  

I think its because you had 2 if statements before the else

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