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

How would you use "_G" and "and"?

Asked by
IcyEvil 260 Moderation Voter
10 years ago

I see those in Scripts Sometimes and I dont know what those are used for Any Examples would be great :D, Thanks.

2 answers

Log in to vote
1
Answered by
Sublimus 992 Moderation Voter
10 years ago

Here's a very basic explanation of both:

_G is used to represent a variable that is shared between all scripts or a global variable. Which can be used for things such as a Global Function

Let's say we had a script that has:

_G.Example = "Yeah"

and we had another script that has:

print(_G.Example)

the output would be: Yeah

The and operator is used when you have more than one argument in an if statement that you want to be true for the code to run.

So for example:

example1 = "Yay"
example2 = 3
if example1 == "Yay" and example2 == 3 then
    print("Woo")
end

the output would be: Woo

Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

If you search for _G you will find several questions already addressing it more in depth than is worth repeating here


and is a boolean operator. It works in a similar way to + or *, except that it is intended to take two booleans instead of two numbers.

a and b is true only if both a and b are true.

So if you require multiple things to be true, you can and them together in an if statement:

if isOff() and time > 10 then

This means that both conditions are met.



We can contrast and to the boolean operator or which requires that at least one of the two is met; either or, or both are true:

if x < -5 or x > 5 then

Finally, there is the unary boolean operator not, which inverts a single value. That is, not true is false and not false is true.



and has a bit more power than just booleans though. While in general you use it on boolean values, and actually works on any values.

Specifically, a and b is a if a is falsy; otherwise it is b.

For or, a or b is a if a is truey; otherwise it is b.

A value is falsy if it is either false or nil. All other values are truey.

The not of a falsy value is true and the not of anything else is false.

Answer this question