Why doesn't "or" work to check for multiple objects?

everytime i use “or”, it never works…

Everytime i use “or”, that line gets skipped, so that even if the obj == one of those words, it will still return true. But if i just did obj.Name == “Bullet” and skipped the rest, it works.

Why doesn’t it ever work? How can i fix this?

Thanks.

1 Like

Instead of writing:

if Obj.Name ~= "Bullet" or "Bomb"

You have to write:

if Obj.Name ~= "Bullet" or Obj.Name ~= "Bomb"
4 Likes

Try adding obj.Name to each check, for example: if Obj.Name ~= "Bullet" or Obj.Name ~= "Bomb"

1 Like

This is your solution, but I’d say you hear me out:

The impracticality of this long if/then check is obvious. You can easily avoid it by using Tables!
What are tables, you ask? Well one of the best features of LUA. You can store data in any way you like and easily look for it with multiple methods.

Simply make a table containing all your items:

local Items = {["Bomb"] = true,["Bullet"] = true, ["Missile"] = true, ["Powerup"] = true, ["Flag"] = true, ["PlaneSeat"] = true, ["Rocket"] = true, ["Baseplate"] = true}

And instead of that long if then statement, you can do

if Items[Obj.Name] then
    -- code here
end

They’re right. Please pardon me. I corrected the script now. Hope I didn’t mess up the formatting!

2 Likes

That wouldn’t work because it would return nil as keys are in numbers and Obj.Name keys are strings.
Use string as keys

{ Bomb = true, Bullet = true, Missile = true, Powerup = true, Flag = true, PlaneSeat = true, Rocket = true, Baseplate = true, }

That would always return true as there’s no value that equals to "Bullet" and equals to "Bomb".
an and would work

Obj.Name ~= "Bullet" and Obj.Name ~= "Bomb"

To OP: do you understand what does the boolean operator or does? String values are truthy in Lua so it always evaluates to true if you use or with them.

3 Likes

I mean he could always just use

if table.find(Items, Obj.Name) then
    -- code
end

Makes it much simpler while not having to add the [Name] = true bool

Also adding in, you don’t need the return false at the very end. If nothing is returned, then the function’s output would be nil (which is equal to false)

table.find is inefficient, it’s better to use the method where you use a hashmap to compare keys to see if it’s valid, as said in a previous post

2 Likes

Yea, inefficient on a large database scale. It is beneficial code wise for something that is like 10 objects and not 10000

1 Like

Relying on this implicit boolean coercion just isn’t considered a good practice.

No it isn’t, nil == false evalulates to false.

If nothing is returned then it wouldn’t actually output anything.

function return_void() end
print(return_void())

Was I supposed to read all of that?

That’s my point.

Since nothing is being returned/outputted, it would be considered be nil. Nil literally means nothing. Nil is nothing. Adding on, I mistyped the part about nil being equal to false. I meant to say that they follow the same principles (in a type of function like the one johnny6288 sent).

For example, take 3 variants of function add():

--1
local function add(n1, n2, n3)
	if n1 + n2 == n3 then
		return true
	end
end

--2
local function add(n1, n2, n3)
	if n1 + n2 == n3 then
		return true
	end
    return false
end

--3
local function add(n1, n2, n3)
	if n1 + n2 == n3 then
		return true
	end
    return nil
end

If you call the function in this way, it will always have the same output, no matter which variant of the function you use.

if not add(5, 4, 10) then
	print("not equal")
end

Again, the way I typed it was wrong (thanks for correcting me), but all I’m trying to say is that he didn’t need a return false at the end of his function.

If I repeat what I’ve said earlier, my point is returning nil when the expected return value is a boolean just isn’t a good practice. So are you implying that he don’t need to explicitally return a value of the expected type and can rely on a implicit boolean coersion instead?

So what if it’ll always have the same result? It doesn’t justify the fact that it’s not a good practice. If I expect a boolean, I explicitally return a boolean.

But why not anyway as the return type is a boolean? Why do you need to rely on an implicit boolean conversion?

No; print takes a variable amount of arguments, if no values are passed it won’t output anything; it’ll print nil if it did return one value of nil.
There’s a difference between return and return nil, implying that it’ll be considered returning one value of nil is wrong.

I was implying that by nothing you mean void am I correct?
This isn’t JavaScript or Python where its nil equivalent are implicitally returned when no return is added.
nil/null is a value. Returning no value doesn’t mean it returns a single value nil in Lua.

My point is that:

  • returning nil isn’t the same as returning no value
  • relying on implicit boolean conversions when you don’t need to just isn’t a good practice.