Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way you can 'reverse' a bool?

Asked by 8 years ago

What I mean by that, is, can you basically get false from a true and a true from a false in one line? I can make a function to do it, but I'm lazy, and I would have to do it for every script.

E.G;

local boolVal = false

while wait() do
    boolVal = bool.reverse(boolVal)
    print(boolVal)
end

-- would spam 'true false true false true false' etc. in output

Thank you!

~TDP

2 answers

Log in to vote
6
Answered by 8 years ago
Edited 7 years ago

Not!

There's a nifty logical operator called not (also known as an inverter, or "not" gate), which inverts any boolean value provided (in the case of Lua however, you can also use this operator on non-boolean values since anything that exists is evaluated as true). Here's an example:

print(not true) -- > false
print(not false) -- > true
print(not not true) -- > true

And as mentioned above, Lua would also support operations like this done on other data types as well:

print(not nil) -- > true (nil doesn't exist, which evaluates to false in this operation, then returns true once inverted)

print(not 1) -- > false (the number 1 exist, which evaluates to true, which is then inverted)

print(not "something") -- > false (same concept as above)

I also have a video on this, if you'd like to check it out here.

0
Thanks! TheDeadlyPanther 2460 — 8y
0
:D OldPalHappy 1477 — 7y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Well, it's pretty simple.

local boolVal = false

while wait() do
    if boolVal then
        boolVal = false
    elseif not boolVal then
        boolVal = true
end

It just goes through to check which one it is, and if its not that one, then make it that one, and since it's looping, in theory, should just swap each time.

  • Sorry, just noticed you said in one line. Now, I have no idea for you there. I'm a mere beginner.

Answer this question