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

Which method of IF statements are better in terms of optimising and efficiency?

As of now, I've been using a mix of two:

method 1:

local condition1 = true
local condition2 = true
local condition3= true

if condition1 and condition2 and condition3 then
	print("all conditions are true")
end

method 2:

local condition1 = true
local condition2 = true
local condition3= true

if condition1 then
	if condition2 then
		if condition3 then
			print("all conditions are true")
		end
	end
end

2 answers

✓ Accepted answer

Hi Marmalados,

According to Roblox Studio, method 2 is faster. I ran multiple tests to prove that and here is the code that I used:

Time Testing Code

method1 = 0 -- Will count how many times method 1 came out to be successful in the trials.
method2 = 0 -- WIll count how many times method 2 came out to be successful in the trials.

for x = 1, 100000 do -- Runs 100 thousand trials on the code.
	t1 = tick()
	
	if workspace:FindFirstChild("Script") and workspace:FindFirstChild("Baseplate") then
	end
	
	t1 = tick() - t1
			
	t2 = tick()
	
	if workspace:FindFirstChild("Script") then
		if workspace:FindFirstChild("Baseplate") then
		end
	end
		
	t2 = tick() - t2
	
	if t1 < t2 then -- If method 1 turned out to be faster then add points to method 1.
		method1 = method1 + 1
	else
		method2 = method2 + 1
	end
end

if method1 > method2 then -- If method 1 had more points then it is best.
	print("Method 1 is best.")
else -- Otherwise method 2 is better.
	print("Method 2 is better.")
end

##I hope I helped and had a great day/night.

Thanks,

Best regards,

KingLoneCat

1 comment

0

Thank u!

The first statement is like, if yes and yes2 and yes3 then runs the script, while method 2 is like if yes then (checkpoint) if yes2 then (checkpoint) if yes3 then (checkpoint) as in consequently checking if they are all accompanied to each other, as in if the first yes is true, then check if the 2nd yes is true, vise versa.

I believe if you want a more-effecient based script, i'd prefer method 2 as in consequently running check-ups while proceeding throughout the script. Like I said, the method 1 is just to check at first if all method's are true to proceed, while method 2 is checking if the first if statement is true, to proceed to second if statement and so on.

2 comments

0

But which method would be less taxing on game performance?

1

Well, game performance really doesn't determine on a script's layout but I believe what you're wondering is what's the best way to not request to much information from the game, which you're not really requesting much here unless you're planning to use RemoteEvents, as in I would say method 1 would be best for that, as it runs it through the first line. The method 2 would just check continuously.

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