Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

what Does "While True Do"Means?

Asked by 10 years ago

I Was Watching A Video About Scripting So I Saw That The Guys Put "While True Do" At The Top Of The Script I Know That "While True Do" Is A Key Word Because I Saw It Turn On Blue When the Guy In The Video Add It To The Script The Video Was About Transparency So I Don't Know What Its Means

2 answers

Log in to vote
-1
Answered by 10 years ago

It's a loop.

while true do
wait(1) 
-- Anything here will loop forever
end

http://wiki.roblox.com/index.php?title=While_true_do

Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Lua normally executes from the top to the bottom exactly one line at a time (first line, second line, third line, and so on).

If you need to do something many times, that's inconvenient: your script would have to be as long as what it does -- if you want something to change color every second for 5 minutes, you'll have 300 lines to change colors (600 if you include the waits!).


To let us not program like that, there are control structures which control the flow of the program to be different from the direct one-more-line procedure that happens in normal code blocks.

The simplest control structure if the if statement which checks to see if something is the case before executing a block of code:

if condition then
    -- This ONLY happens if
    -- `condition` is met
end

These let us make decisions.


Another type of control statement is a loop. These are called "loops" since after execution gets to the end of them, they zip back up to the top.

A really simple one is the while loop which acts similarly to an if statement, except repeats as long as the condition is held.


true is the name for the constant boolean value which passes control statement conditions. false is the complementing value that fails conditions (all values except nil and false pass conditions; nil and false are the only two values which fail conditions)


while true do then refers to a while loop that has a condition that is always true -- so the loop will always keep chugging on; once it reaches the end, it starts again from the beginning of the loop.

Whatever code appears inside of it will repeat indefinitely. Because of this, it's called an infinite loop.



There is another control structure, the break statement which "breaks" loops. This means the execution jumps out and to the bottom of a loop whenever you hit the break. Since this interrupts the loop, these will usually appear inside some if condition.

Answer this question