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
It is because these “text boolean values” aren’t what you think they are, these are just strings, and strings always evaluate totrue (even when empty), even if the content is False.
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
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