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

Should I store "false" as boolean value table or will using "nil" be more efficient?

Title. Right now, I have several true/false values that I evaluate from ModuleScripts. Unless I'm going to be iterating over these, it seems like storing "false" as a value wastes space as I could just evaluate

if bar then
	-- do stuff
end

instead of

if bar == true then
	-- do same stuff
end

Am I correct in this optimization?

47 comments

1

wonderful question

1

wonderful question

1

wonderful question

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

1

i hope my answer helps

0

who upvoted all those comments

0

who upvoted all those comments

0

You shouldn't worry about very small optimizations unless you really need to.

2 answers

if bar then
    -- do stuff
end

would 100 percent work better

2 comments

0

in all they would work the same but i use if blank then as it is easier and maybe more efficient

2

if isValid then is easier to understand than if isValid == true then

They can essentially be the same thing.

For example, when checking a boolean value:

local bool = true

Both would check to see if bool == true:

if bool then
if bool == true then

By doing if bool then, you're basically telling the conditional statement to check for bool == true in this scenario. If you did this...

if not bool then

...then you telling the statement to check for not true, which is essentially bool == false. Efficiency is, therefore, almost entirely ignored when doing this.

When checking to see if a userdata exists, you can then feed the condition the object directly:

local inst = script.Parent:FindFirstChild("Humanoid") -- FindFirstChild() will return the Instance if it finds it, or nil if it can't find the Instance
if inst then

In this scenario, if inst then is equivalent to if inst ~= nil then, which can also be viewed as if (inst ~= nil) == true then since inst ~= nil will evaluate to either true or false.

"Efficiency" is not a relevant factor here. It's just simpler to do either one of these things:

if inst then
if not inst then
if bool then
if not bool then

As compared to:

if inst ~= nil or bool == true then
if inst == nil or bool == false then

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