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

How does "and not" work for a bool value? [closed]

Asked by
Seyfert 90
7 years ago

This question already has an answer here:

What is an "if not" statement?

For example


local ran = false for i, v in pairs(game.Workspace.Model1:GetChildren()) do local intValue = v:FindFirstChild("DataValuel") if intValue.Value == 0 and not ran then ----do a thing ran = true; end --// do other things end end

What does Lua and not ran interpret as? I do not understand. ran is set to false, so and not ran would mean and not ran = false?

0
If the if statement had been and not ran = false rather then and not ran you'd be correct. Since it has no value to compare it to, it assumes you are checking if that value is there at all. Since it's technically set to false, the code counts it to nil. Therefore what the if statement is saying is "and ran = nil". CorruptScript 86 — 7y

Marked as Duplicate by TheeDeathCaster and cabbler

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 7 years ago

For a Lua conditional statement to be truthy, the condition must evaluate to a value that is not false nor nil. The not keyword returns the boolean complement of the value that it precedes; if the value is truthy, then preceding the value with not will return false because the complement of a truthy value is a non-truthy value, or false. If the not keyword precedes a non-truthy value (false or nil), then true will be returned because it is the only truthy boolean value that exists in Lua. Note that the not keyword will always return a boolean.

not ran is interpreted the same way. Since ran refers to false, preceding it with not will return true. If ran were to refer to true, then the condition would evaluate to false because not true is equivalent to false.

So, the conditional statement in question (if intValue.Value == 0 and not ran then) will only have its body ran if those two statements evaluate to true because you're using the and keyword. Think of reading it as "if the Value property of intValue is equal to 0 and the complement of ran is equal to true then..."

0
I forgot to mention that when `== true` is omitted, a truthy value is searched for rather than the literal value `true`. Unsubtleties 31 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Hey Seyfert,

The method 'not' works as the opposite of what you want. So, when you check if a bool value is not what it is right now, it means that it is check for the opposite of that value. So, if you check if true is not true, then you are checking if it's false. I most of the time use not to check if a UserData Value exists or not. Which returns a boolean value. Here is where you can find out more about the not method and below is just a personal example.

Example A

-- Using Boolean variables inside the script --
local bool = true

if not bool then
    print("It's false.")
    bool = true;
else
    print("It's true")
    bool = false;
end
--[[
 The above works like a toggle. When the bool value is true, it will run the second line of code and when it's false it will run the first line of code. 
--]]

Example B

-- Using Booleans to check if the UserDataValues exist --

local part = workspace:FindFirstChild("Part")

if not part then
    print("The part doesn't exist.")
end
--[[ 
 The above will check if the part exists and if it doesn't then it will print "The part doesn't exist."
--]]

Well, I hope I helped in one way or another. Have a great day/night.

~~ KingLoneCat

0
So how come my statement "if not ran then" is running if I set local ran to false with local ran = false or is that just a placeholder and not actually setting it to false? Seyfert 90 — 7y
0
Since `ran` refers to the value `false`, you can think of `if not ran then` as `if not false then`. The `not false` will be evaluated first; therefore, this changes to `if true then`. Remember that a condition needs to be truthy in order for it to run, and since true is truthy, the body of the conditional statement would run. Unsubtleties 31 — 7y
0
I am sorry I am really just not grasping the concept. I don't know why, are there any videos about this? I have ran set to false with local ran = false. Then the if statement runs, "if not ran then" which meeans if ran is not false OR if ran is true then run the rest of the code? Right? However, my code that I have is running the lines. I am too confused I need a video or something this does not m Seyfert 90 — 7y
0
Wait a second I think I may be onto something. For bool values, simply "if ran then" is checking if the bool value is true? and "if not ran then" is checking if the bool value is not true, or false? Regardless of the actual value of the bool? If so then I think I get it now. I was getting confused before. Seyfert 90 — 7y
View all comments (3 more)
0
Ahhh, so normally when you do if statements you would have to do if BLAH == whatever but because bool values only have two values, you can simply code if BLAH then and that defaults to Lua trying to figure out whether BLAH is true and if not BLAH then defaults to Lua trying to resolve if BLAH is false yes? Seyfert 90 — 7y
0
You're getting closer. It's not strictly checking if `ran` refers to `true`; it's checking if `ran` refers to a truthy value, which is defined as any Lua value that is not `false` nor `nil`. `if not ran then` will evaluate to true if `ran` refers to either `false` or `nil`, because that's how the `not` keyword behaves. Unsubtleties 31 — 7y
0
Look at my original reply again. `if BLAH then` will evaluate to true if `BLAH` is neither `false` nor `nil`. Unsubtleties 31 — 7y