Skip to main content

Computer program

A computer program is a sequence of instructions that a computer can execute or interpret. A script contains human-readable text that represents the instructions of a computer program.

This program causes a door to close and open repeatedly, at intervals of 2 seconds.

function onStart()
while true do
Door.close()
Task.wait(2)
Door.open()
Task.wait(2)
end
end

The above program is made of several elements, which are outlined below.

function onStart()   <-- Declares a global function named 'onStart'
while true do <-- The start of a 'while' loop block - the code between 'do' and 'end' will repeat forever
Door.close() <-- Calls the 'close' function that is part of the Door module
Task.wait(2) <-- Calls the 'wait' function that is part of the Task module
Door.open() <-- Calls the 'open' function that is part of the Door module
Task.wait(2) <-- Calls the 'wait' function that is part of the Task module
end <-- Closes the 'while' loop block
end <-- Closes the global onStart() function

This Lua Primer will cover all of the concepts required for you to fully understand each element of this simple program and more, divided into the following sections: