Why do if statements break whenever i check for an existing item in-game

If you’re checking for an in-game part or any other thing using an if statement, If that part doesn’t exist, The script will not resort to elseif, Instead it will completely break. even pcalls don’t work. for example

prox.Triggered:Connect(function(player)
	if game.Workspace.part then
		--Do Stuff
	else
		--Do stuff
	end
end)

The part doesn’t exist as of now, But the script doesn’t care, It’ll just ignore it. and the output will just say

15:49:42.432 Part is not a valid member of Workspace

Is there any alternative to this or any way to make it turn to the else function instead?

“Part is not a valid member of Workspace” is not just any output, it’s an error. The script will stop running if it runs into an error.
If you want to check for the part’s existance without getting an error in case it doesn’t exist, use:

if game.Workspace:FindFirstChild("part") then

Huh. I always wondered what the difference was between FindFirstChild and WaitForChild, Thanks.

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