Advanced “Hello World!” in Lua
I started learning Lua today. It’s used to script extensions and automations in the Moho animation software I’m considering. Since I forgot my earbuds, I wasn’t going to be able to watch my video lessons at the coworking space, so I decided to explore Lua as I’m also trying to get my teenager to learn it for Roblox.
Since I’m an experienced developer, just doing “Hello World!” wasn’t going to be enough, so I wrote a console script that asks your name, gets it from the console input, and then repeats it 10 times (to try their way of looping). I commented it heavily not for you, but for my reference when I come back to it. But I thought I’d share.
A good deal of it is me noting the differences between it and JavaScript, like mentioning function hoisting. Hope you find it useful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function Getngreet () -- no function hoisting, so functions must be declared before they're called. local myname = io.read() -- get user input from terminal and assign it to the var for i=1,10,1 -- for(i=1; i<=10; i++) do -- start the loop print("Hi " ..myname.. "! - " ..i) -- use two dots around var names instead of single dots or pluses print("Yo ", myname, "! - ", i) --[[ This is a multi-line comment, like /* */ in JS Note how the commas create a 5-space gap while the '..' is inline, no excess spacing --]] end -- evaluate and loop or end end -- end of function (like closing brackets) print("What's your name?") Getngreet() -- call the function |