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

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

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

✓ Accepted answer

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.

2 comments

0

Thanks!

0

:D

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.

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