Learn to Code via Tutorials on Repl.it!

← Back to all posts
Lua Economy System Tutorial
JosephFrey1 (1)

My Lua Tutorial!

Lua is a great language, it can do most anything!
This tutorial was made by JosephFrey1!
While thats enough Chit Chat! Lets get right into it!

Basics!

First lets talk about the really basic stuff!

This is how you make a one line text:

-- This is a one line text!

However to make mutiple line texts use this:

-- [[
  This is a multi Line text!
]]

There are 2 ways to print things!
One is:

print("Hello World!")

Another was is:

io.write("Hello World!")

Either way works!

There are 2 ways to set a variable. Both are fine for what we will be making!

local variable = "Im setting a variable!" -- Option 1
variable = "Im setting a variable!" -- Option 2

To read answers use this:

yourvariablename = io.read()

If statements

Here is an example:

if variable == 0 then
end

Instead of brackets to mark when the if statement is done Lua just uses a then and end system.

Math

Lua can do math!

Simple Example:

local variable = 0

if variable < 7 then
  print ("Our number is smaller than 7!")
end

Loops

Loops are simple!

Just use the example:

while(variable < 7) do
  print("Our number is below 7!")
end

Economy

Lets make an economy system using what we just learned!

First lets make a money variable using what we just learned!

local money = 0

Lets make a loop using:

while(money < 100) do

Now lets use this variable in our code! To print a value use .. variable .. as demonstrated below
\n Splits the text

io.write('You need money or you will loose your house!\nCurrently you have ' .. money .. ' dollars in your bank account What would you like to do?\n1. Work\n2. Rob someone\n3. Rob a bank ')

First lets create the work option!

if what == '1' then
  io.write('Big Bux!\nYou earned 5 dollars\n')
  money = money + 5

Thats simple right!

Now lets create the rob someone option!

elseif what == '2' then
  io.write('You rob someone!! But they called the police. You collect only 2 dollars!\n')
  money = money + 2

Isnt that simple!

Finally lets create the rob bank option, this one gets a but more complicated.

elseif what == '3' then
  io.write('Wow!!\nYou make the plan and succeed, but you did not time it well. The bank had just ran out of money. They take all of your money and a punishment!\n')
  money = money - money

Basically what we did there was we subtracted money by itself so it would equal 0 no matter what.

Wrapping up

Final code:

local money = 0
while(money < 100) do
io.write('You need money or you will loose your house!\nCurrently you have ' .. money .. ' dollars in your bank account What would you like to do?\n1. Work\n2. Rob someone\n3. Rob a bank ')
local what = io.read()
if what == '1' then
io.write('Big Bux!\nYou earned 5 dollars\n')
money = money + 5
elseif what == '2' then
io.write('You rob someone!! But they called the police. You collect only 2 dollars!\n')
money = money + 2
elseif what == '3' then
io.write('Wow!!\nYou make the plan and succeed, but you did not time it well. The bank had just ran out of money. They take all of your money and a punishment!\n')
money = money - money
end
end

Have a great day and thank you for reading my tutorial!

Comments
hotnewtop
CodeMaster007 (108)

Wow! This is a really great tutorial. It goes in debt on Lua and seems good. I was hoping to learn Lua sometime soon on Repl.it and apply my skills to roblox. Do you think this Lua and the one on Roblox are the same? If they are, how does it effect the characters and how they move. Thanks,

CodeMaster007

JonathanSutton (5)

@CodeMaster007 I think that Roblox kind of has their own version of Lua called ¨roblox lua¨ I don't know that much about Roblox Lua (or even normal Lua) so I'm not sure how it affects the characters and how they move.