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

Does the ordering of an IF statement matter?

Does the order in which you write a if statement affect the way it runs?

I have this code, which would error if it was written this way:

if closestPart.Contents.item.Value == "" and closestPart then return end

But would work fine if written this way:

if input.KeyCode == Enum.KeyCode.F and active == false and closestPart then

Does switching the order change the outcome of the if statement?

Thanks

1 answer

✓ Accepted answer

Yes. Boolean expressions like that are shortened if chains.

if closestPart.Contents.item.Value == "" and closestPart then return end

equals

if closestPart.Contents.item.Value == "" then
	if closestPart then
		return

because Lua stop checking once it knows the outcome.

Of course checking if closestPart exists last.. makes no sense, same for the expression.

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