Skip to content
Sign upLog in
← Back to Community

Lua - Part 3: Input and Output

Profile icon
Antelear

Hey, vsauce! Opal here.

What is input?

Essentially, input is just giving information in exchange for output, or an expected outcome! So, if I wanted to make something like a game where I say their name, input will come in. Now, let's try this out!

local sentence = io.read() print(sentence)

Now, let me explain what 'io' basically means: it's just input-output. So, an easy way to remember is that we give the code an i, which is input, and it gives us an 'o', which is the output.

Input - this is a sentence! -Output - this is a sentence!

What can I even do with this information??

That's a very good and easy question to answer! Let's find out..

local name = io.read() print("Hello, "..name.."!")

Okay, let me explain the dots! I'm basically connecting this variable (which is a string) to the print message. So, what's the output?

Input - Opal -Output - Hello, Opal!

Now, this isn't the only use we have for input, as you can even use it for, i don't know, a typewriter, or maybeeee, a calculator! We'll learn how to make a calculator in the next part, so stay tuned. ;)

How can I use this?

Now HERE'S where the fun begins! >:) Let's make a quick character counter......No not video game characters, silly! Lemme show ya:

local text = io.read() print(string.len(text))

What does string.len stand for? Well we're asking for a string (which is a type of class), then we're asking for the length of the string! (len). Should make sense.. Anyways, it'll count the characters in the string, and return a value based on the amount of characters there are in the string. Simple and easy!

"This is cool and all..But where's the fun part?"

I'm about to get to that now! I was just teaching what you could use with io.read. Now let's break this up step by step, so you can follow along. I won't give anymore comments, except inside the code blocks ;3

1.

local text = io.read() -- Making the Input for i = 1,#text do -- for every Character between 1 and the text do.. end

2.

local text = io.read() for i = 1,#text do print(string.sub()) --substring; a contiguous sequence of characters in this string.. end

3.

local text = io.read() for i = 1,#text do print(string.sub(text, 1, i)) -- 1st one = checking the amount of characters in the string. 2nd one = adding the amount of characters printed by one. 3rd one = repeat until it hits the last character of the string. end

4. Results

Input - Hello there! -Output - H He Hel Hell Hello Hello Hello t Hello th Hello the Hello ther Hello there Hello there! -- Pretty cool isn't it?

Conclusion..

I love input, yknow? It's a very interesting and wonderful concept! You can really literally create anything off of input, like a simulator, stock market game, I could go on and on..Ah, input!

I hope you all have a nice november, and staying strong! >:3

Goodbye!!

Voters
Profile icon
DynamicSquid
Profile icon
Baconman321
Profile icon
Antelear
Comments
hotnewtop
Profile icon
MuhammedCeesay1
Profile icon
DynamicSquid

Oh nice! We don't see a lot of Lua tutorials here!

Profile icon
Antelear

@DynamicSquid
Haha, yeah! I've been scrolling through the actual lua tutorials out there, and literally none of them help me for a specific thing in learning, so I thought to myself.. "What if I just make them myself?"

And so I did! I've already made 3 parts, and the next part should be either different programming languages (and how they work similar to lua), or just a simple new method in scripting! Most of the tutorials I make are referenced off Roblox Studio.. I'm sure you know what that is by now.. Haha-

But anyways, it's fun making these tutorials, and helping others learn as Iearn too! I'm not even experienced in lua either, I just teach what I know..

And sorry that I'm rambling, it's just that code just excites me! The fact that code runs the very thing that's in my hand, the thing in typing on to you now, my phone, its just incredible!

The fact that a phone can only detect human inputs such as the fingers... Its just, well, exhilarating to know that science and technology gone this far... Its almost scary, actually..

And all these amazing and more excellent programming languages such as LOLCODE, or that could be the name.. Or even "Ruby" because the creator thought python was too complicated,thr very fact we can create a language that a computer can understand with full customisation...?

I could go on and on.. AND THE FACT THAT YOU COULD EVEN PROGRAM A WHOLE VIDEO EDITING SOFTWARE, LIKE DAVINCI RESOLVE?.... man, I just can't believe how far we've gotten in society, to this point. That a simple meme was actually a lot of work behind the scenes.. And it's all thanks to code. The very thing that keeps us alive in general!

.... Oh my, I have rambled quote a lot, haha-.. Sorry if you found it boring, I just carry myself away sometimes hehe-.

Well, I'm glad you liked this tutorial, I put a lot of work into them, and it's nice people appreciate my efforts. I hope everybody has a good day, or night, depends. Anyways, nice coding developers!

Profile icon
Baconman321

Nice, but what does the # represent in #text?

Profile icon
Antelear

@Baconman321
well, you know the default values for I loops?

for i = 1,5 do....

Well, By using #text I'm counting all the characters in the string, so it'll go through of all of the string in the string.sub command. Hope that helped

Profile icon
Baconman321

@Opalmime
Oh, ok

Kind of like the text.length in other languages like JavaScript.

Yep, I got this confirmed: https://stackoverflow.com/questions/17974622/what-does-mean-in-lua

Profile icon
CSharpIsGud

@Baconman321
You can also do

for i, v in pairs(text) do print(v) end

and

for i = 0, 1, 0.1 do print(i) -- 0, 0.1, 0.2, 0.3, ..., 1 end

print "Hello World"

Profile icon
Antelear

@CSharpIsGud
This is not what I'm intending, but okay..-

Profile icon
Antelear