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

What is the point of "not" in an if then statement if there is "~="?[And other questions]

Asked by 9 years ago

You see, to see if something does not equal true or is not nil you use ~=. Like as seen in this example:

if workspace.Bob.Name ~= "Joe" then --If Bob's name does not equal Joe then...
    print("Bob is not Joe")
end

But this would also work with not.

if not workspace.Bob.Name == "Joe" then --If Bob is NOT Joe then...
    print("Bob is not Joe")
end

I hope someone answers. This has been bothering my. Anyway, back to question 2.


Why does the script error when you try to run a code after a return? For example:

function vine(foo)
    if foo == "9+10" then
        return "21"
        print("This is one of my favorite vines") --ERROR ERROR THIS IS AN ERROR!
    end
end

print(vine("9+10")) --This will print 21 because of the return

Why does it error?


Why does Color3 say it is 255,255,255 when 1 is the same as 255?

Like, you see gui's colored 255,255,255 for white, but in a script, to change a gui or something to white you need to do Color3.new(1,1,1) 1,1,1? Why not just make it 255?



Thanks!

EDIT:

What is the point of in pairs when there is next?

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Not

There's more than just == to get something that's true or false.

Even then, you could still get rid of the not: not (a < b) is the same thing as a >= b (usually). But do you want to? Is it actually clearer always?

It can be annoying to eliminate these. What's not (a == b or c > d or (e ~= f and not d))? If that's the formula you came up with, is it really clearer to get rid of not?


not also makes for better style.

Usually, you should say if gui.Visible then rather than if gui.Visible == true then because the == true is entirely redundant and makes you have to read more to understand what's going on -- if you don't have == it makes it clear that you either expect true or false and not anything else.

If you want to say it's not visible, you shouldn't say if gui.Visible == false then, you should just say if not gui.Visible then. Doesn't that read nicer?

It's simpler and clearer to sometimes allow you to use not. It also can be used to coerce other values into true and false: not not nil is false and not not 5 is true.


Code after Returns

You can't have code after a return because the return means it's going to leave the function there -- the code after it could never run, since it just stopped.

Lua errors immediately about this because you've probably made a mistake -- you want the print to be before the return so that it can actually happen.


Colors

This is just a weirdness. There are many different notations for Color. One common one uses 0 to 1 for red, green, and blue, and another one uses 0 to 255. They're obviously equivalent, it's just that one is 255 times bigger. The properties tab uses 255, and the implementation of Color3 in Lua uses 1.


in pairs

Most languages let you do many things in several different ways. Lua actually does this much less than most other languages.

pairs(x) is the same thing as next, x, but that is completely opaque. It's much clearer to use a single expression than a tuple.

Because of how for loops work, having next is necessary. However, that's not the best way to write your code -- so pairs is provided, which is what you should use.

0
you just knocked ou me :p davness 376 — 9y
Ad
Log in to vote
1
Answered by
davness 376 Moderation Voter
9 years ago

Q1:

something = false
if not something then
--Code here
end

if not is useful on if bool statements: it works when a bool condition is false

Q2: Returnings are the last thing a function can do. Being specific, you can do all the code, but if the function will return something, it must be put at the end of it:

function vine(foo)
    if foo == "9+10" then
        print("This is one of my favorite vines")
        return "21"
    end
end

print(vine("9+10")) --This will print 21 because of the return

Q3: I do not know why must we to use a range of 1 instead of 255. But its easy to override this problem.

script.Parent.Color = Color.new(0.8, 0.6, 0.78)
--or else
script.Parent.Color = Color.new(200/255, 180/255, 195/255)
-- it uses simple arithmetic (division)
0
What about question 4? EzraNehemiah_TF2 3552 — 9y
0
when i answered you already edited it davness 376 — 9y

Answer this question