How to make 'if' statement run properly when instance is nil?

How would I make this ‘if’ statement run properly when char.Passport isn’t a thing?

im getting the error “Passport is not a valid member of…” when the passport tool isnt equipped which is causing the ‘elseif’ to not run

script.Parent.Triggered:Connect(function(player)
	
	local char = player.Character
	
	if char.Passport then
		script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
		wait(1)
		script.Parent.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
	elseif not char.Passport then
		script.Parent.Parent.BrickColor = BrickColor.new("Really red")
		wait(1)
		script.Parent.Parent.BrickColor = BrickColor.new("medium Medium stone grey")
	end
	
end)
3 Likes

Just do “if char and char.Passport”

Autocorrect is annoying lol

3 Likes

you can use char:FindFirstChild(‘Passport’) that will return Instance otherwise nil
heres an example:

if char:FindFirstChild('Passport') then
print('found a passport! :)')
else
print('no passport')
end

Also avoid these things:

BrickColor.new("medium Medium stone grey")
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.