Why my Condition doesn't work correctly?

Hi guys, today I’ve try to use a condition to make my weapon selection when the client select a weapon on my custome backpack. But when I select for the first time, all is good, when I again click on the same button it’s work, fine. When I want to select the second when the first was be selected, that unselect all weapons instead of selected the second. Anybody can help me to fix that?

print(backpack.PlayerEquip[2] == nil and findItem.Name or nil)
backpack.PlayerEquip[2] = backpack.PlayerEquip[2] == nil and findItem.Name or nil

PlayerEquiped is a table in my backpack table. findItem is the item who want select. No the two weapons doesn’t have the same name.

https://gyazo.com/0b085a3f69a298db8194fcfe0b038065

Thank you everybody to have get the time to read this question.

1 Like

In your print function you are running a check, I’d suggest running the check out of the print function.

Other then that, I do not understand your question.

And I dont understand your answer :thinking: The print is good loll, that’s the condition malfunctioning. Sorry if the question is not good, i’m not originally english. I do what I can to express myself.

After looking into it more, I think it has something to do with setting findItem. If findItem is your tool it should be printing the name of the tool, look over your code and ensure that findItem is being set properly.

Yes, no problem with that. Check the OutPut, when I select the first weapon that’s print Default and when I select the second that’s print Default2… So I dont think that’s the problem :confused:

Oh I just had to re-evaluate your wording, my apologises. After re-reading, I understand.

1 Like

You seem to not fully understand how the “pseudo-conditional operator” works.

x and y or z

x and y is evaluated first because and has higher precedence. This evaluates to x if x is falsey (false or nil), y otherwise.

Observe

print(nil and 5) -- nil
print(true and 7) -- 7
print(1 and false) -- false

I will use parentheses to show operation order:

(x and y) or z

Let’s assume x is truthy:

y or z

y or z evaluates to y if y is truthy (not false and not nil), z otherwise.

The x and y or z idiom is used to replicate C’s ternary conditional operator. But it fails when y is falsey because of the nature of and and or.

// C++ example

#include <iostream>

int main() {
    std::cout << (false ? "Falsey!" : "Truthy!") << "\n"; // outputs falsey!
    std::cout << (false ? 0 : 1) // outputs 0.
    // even though 0 is falsey,
    // the ?: operator always returns the second operand if the first is false. Thisis where Lua's pseudo-?: fails.
}

Example of this failing in Lua:

print(true and false or 6)

This results in 6. You might want to not do this conditionally and use an explicit if-statement to help clarify.

4 Likes

Thank you for this explanation, do you think I shouldn’t use a condition or just i’m not ready to exploite that?

Using an if-statement might help you understand it better. And you’re not really exploiting (do you mean abusing?) it. One-line conditionals every now and then aren’t bad. Abusing them is bad. Doing it everywhere and/or chaining them.

a and b or c and d or e -- Please don't do this.

Take shortcuts every now and then if it won’t hurt readability/performance.

1 Like

Yes I’ve test that before this comment and it’s work!!! Omg thank you for your great explanation now I understand a little bit more how to use a condition!! It’s amazing

1 Like
backpack.PlayerEquip[2] == nil or backpack.PlayerEquip[2] ~= findItem.Name and findItem.Name or nil

But now one time on two that’s give me a «true» :rofl:

Okay!! Now all is good thank you all:)))

backpack.PlayerEquip[2] = (backpack.PlayerEquip[2] == nil or backpack.PlayerEquip[2] ~= findItem.Name) and findItem.Name or nil