Why do If statments use 2 equal signs?

not sure what catagory to post this under, but yeah

Im a decent scripter, and a question ive had throughout my learning experience is why do if statments use 2 equal signs?

like variables use 1, but why do ifs use 2? nothing else uses 2…

if fart = true then
 farting.Value = true
end

^^Why cant we have this :slightly_frowning_face: ^^

Im not mad, just curious lol

3 Likes

The double equals sign is a comparator, while the single equals defines something. This allows you to write code such as

local foo = "example"
local blah = foo == "example"

This wouldn’t make sense if both used the same symbol. The case you described in your post would be attempting to set the fart variable to true, which the Luau interpreter doesn’t understand.

Also pro tip: Luau truthiness allows you to do if x then to check if a variable is not false or nil, rather than if x == true then.

1 Like

im tryna set the fart variable to true!!!
(im very immature)

Setting a variable isn’t a valid statement (something that returns true or false), so it isn’t possible.

as @eemsias said, == is a comparator to check if a is equal to b, where a and b represent variables.

In contrast, when you use =, you’re inserting something a value. = is the same thing as equal to.

In short, with the two equals you’re checking if something is equal to another thing, while with the single equal you’re implementing a value to it:

local a = "I'm a string!"
local b = "I'm a string2!"

local boolean = a == b --Would print false. It is the same as storing the boolean of "a == b", checking if they are the same.

if boolean then
print("They're the same thing!")
else
print("They're not the same thing!")
end

well there are multiple reasons, it makes coding simpler if you want to use other things such as

if fart then -- if fart exist
end
if fart == true then -- if fart is true
end
if fart == false then -- if fart is false
end
if fart <= 3 then -- less than or equal to 3
end
if fart >= 3 then -- more than or equal to 3
end
if fart ~= 3 then -- is not 3
end

It just makes scripting easier.

== means ‘equal to’

e.g

if 10 == 5 * 2 then
–code
end