If statement running even though it isn't supposed to

Hi all,

I’m writing a simple maze generation algorithm, but I’m running into a problem in this section of code.
The if statement says that if target == “Front” and copy.Name is not end, then the if statement should run, printing copy.Name along with a bunch of other stuff.

However, if we look at the output when ran, we can see copy.Name printed as “End” even though the if statement requires copy.Name not to be end to run, along with an error caused by this. I’m not sure what could’ve caused this to occur, so any help with this would be appreciated.

Snippet of code + output window

if target == "Front" and copy.Name ~= "End" then
	generatePart(copy.Connectors.Front.Position, length + 1, false, rotation)
	print(copy.Name)
	--copy.Connectors.Front.Depth = copy.Connectors.Front.Depth + 1
	copy.Connectors.Front.Occupied = true
end		

1 Like

We need more of your code because everything looks fine. However, I do see some things that could be improved that might fix your code. Here are some updates I made, although there are probably more.

if string.lower(target) ~= "front" or string.lower(copy.Name) == "end" or not copy.Connectors[target] then return end
generatePart(copy.Connectors[target].Position, length + 1, false, rotation)
print(copy.Name)
--copy.Connectors.Front.Depth = copy.Connectors.Front.Depth + 1
copy.Connectors[target].Occupied = true

The first part of your if statement will always be true. You are checking if the string.lower is the same as Front, which has an uppercase.

That was a mistake. I did not intend to do that.

it is telling you your error … that name isn’t part of that folder. You got something wrong with the spelling or placement.