If statements & While Looping

Good afternoon, I’ve been practicing Arrays/tables and wondered why “While” loops only work properly when using a text version of true/false? When I try make it the official way, using boolean, the first part works fine but the else if doesn’t work. It doesn’t give me errors either. I’ve left two examples…

This version of the code works fine… But I’m using “Text” boolean values

local Enabled = "True" --Variable "Enabled" == loop status

while Enabled do
	
	if Enabled == "True" then
		local sp = {script.Parent, 2, 4, 5}
		print(sp[1]:GetFullName())
		table.remove(sp, 1)
		print(sp[1])
		wait(1)
	elseif Enabled == "False" then 
		print("Loop disabled")
		
		wait(1)
	end	
end

This version of code is how I was trying to do it. The first part works if true but when changed to false. It doesn’t print “Loop Disabled”

local Enabled = false --Variable "Enabled" == loop status

while Enabled do
	
	if Enabled == true then
		local sp = {script.Parent, 2, 4, 5}
		print(sp[1]:GetFullName())
		table.remove(sp, 1)
		print(sp[1])
		wait(1)
	elseif Enabled == false then 
		print("Loop disabled")
		
		wait(1)
	end	
end
1 Like

It is because while does not procceed with execution when the statement is false. You must check outside, as the thread waits for it to break/finish.

However, if I use text boolean values it works…

It is because these “text boolean values” aren’t what you think they are, these are just strings, and strings always evaluate to true (even when empty), even if the content is False.

1 Like

Just change the while Enabled do, to while wait() do

You would then need to add a break inside assuming they need to exit out of it.

He never said that so ya…

This text will be blurred

Ah right I get you, I’m pretty much telling it what to say with text boolean values, that makes sense.

Even empty string literals are recognised as ‘truthy’ by Lua/Luau.

print("" and true) --'true'

Oh yeah, I meant to type even when empty not unless empty

Did you reply to the wrong… thread?

1 Like

No, but I was reading some questions about admin command and helping people there so got lost a bit, the first part is related tho.

use instead:

local Enabled = false --Variable "Enabled" == loop status

while not Enabled do
	
	if Enabled == true then
		local sp = {script.Parent, 2, 4, 5}
		print(sp[1]:GetFullName())
		table.remove(sp, 1)
		print(sp[1])
		wait(1)
	elseif Enabled == false then 
		print("Loop disabled")
		
		wait(1)
	end	
end

No, Why? This code won’t even work.

Ik i just wanna explain the first while loop issue

I alrady explained in a previous post, and this doesn’t even solve the issue.

I’m just giving an example so please bro

It still does not solve anything and therefore fails to be an example. And how is it an example if you told them “use this”?

I was just trying to help, please relax

Ok so if u wanna control a while loop u could use the break function and change the while loop to task.wait so it not will depend from the variable value

local Enabled = false --Variable "Enabled" == loop status

while task.wait() do
	
	if Enabled == true then
		local sp = {script.Parent, 2, 4, 5}
		print(sp[1]:GetFullName())
		table.remove(sp, 1)
		print(sp[1])
		wait(1)
	elseif Enabled == false then 
		print("Loop disabled")
		break
	end	
end
1 Like