Lua coding

mail@pastecode.io avatar
unknown
lua
2 years ago
12 kB
1
Indexable
Never
-- #1 Output and comments
-- The script ignores comments and comments are useful for coding to tell you what the code is doing
-- printing is also useful for debugging your code if you get a error or expection you can put print statements to see if it prints
-- if it does not your error is most likly in that line.
-- This is a single line comment
--[[This
is
a
multi
line
comment
!]]

-- Printing Text on the screen
-- print - this is a keyword
-- print() - this is a function
-- "" or '' - these are both strings
----------------------------------------------------------------------------------
-- Examples of printing
print("This is a string aka test and a print statement that is printing to the output")
----------------------------------------------------------------------------------------

-- Concatinating Strings
-- to conact it is basicly just a space in lua
-- this is a concat aka a space in code using the .. or add two strings together
print("Concatinating ".."this string")
print("Hello ".."jack".." you are ".." 24 ".. " Years " .." old ".."today".. ".") -- this is Concatinating aka adding spaces
-------------------------------------------------------------------------

-- #2 Data Types and Variables

--[[1. nil - nothing,empty
--  2. Number 0.1,4,5,6,1/2,1.5,5
--  3. String "" or '' string is just a word or Text
--  4. boolean Yes or No True or False
--  5. Tables Arrays,dictionarys and arrays and oops starts with {}]]

----------------------------------------------------------------------------------------
-- Craeting local Variables
-- Variables store all data types and hold values that can be used later on just an container conatining the values
-- Variables can not start with number or special characters execpt _ underscores
local number = 
print(number) -- nil because no value has been assigned

local numbera = 5
print(numbera) -- 5 because that is the value that has been assigned
----------------------------------------------------------------------------------------
-- Examples using Variables
local name = "phil"
print("My Name is" .. name .."i'm 18 years old i decided that my name should be phil because the ..name .. sucks")

name = nil -- this can delete the name
print(name) -- will print nil because the value is nil
----------------------------------------------------------------------------------------
-- Adding strings
local Name = "Phil"
local Last_Name = "Smith"
local full_name = Name .. " " .. Last_Name -- prints both varibales 
print(Name .. " " .. Last_Name) -- prints the varibales first and last name
print(full_name) -- prints variables full name

-- false and nil are no
----------------------------------------------------------------------------------------

-- Global Scope varibale
 A = 50 -- this is a global variable which can be acessed anywhere
_G.String = "Smith" -- same with this
print(String)
print(A)
local a = 50 -- but this is a local varible which is limited on where it can be acessed from
print(a)
----------------------------------------------------------------------------------------
--Getting type of a varibale

print(type(A)) -- will print number because that's the type of A

----------------------------------------------------------------------------------------

-- #3 Math 

--- Converting a string to a number

local y = 8
local Y = 10
print(type(y)) -- this will print a number
print(type(tostring(Y))) -- converts the number into a string will print string
print(type(Y))
----------------------------------------------------------------------------------
-- converting a string to number

local String = "Hate"
print(type(String)) -- this will print string because this is a string
print(type(tonumber(String))) -- will print the string a number because it has been converted to a number
----------------------------------------------------------------------------------
-- Math operations in lua
--[[+ additon
    - subtraction
    * multiplication
    / divison
    ^ square to the power
    % Remainder of a number
    ]]
print(5+5) -- returns l0
print(5-5) -- returns 0
print(5*5) -- returns 25
print (5/5) -- returns 1
print (5^5) -- returns 3125
print(5*9/(2+5)) -- doing PEMDAS 
print(5%1) -- returns the remainder 
----------------------------------------------------------------------------------------
-- Using the Lua Advanced math libery

--print(math.pi(15)) -- prints out the pie of a number
math.randomseed(os.time()) -- makes it more random
print(math.random(15)) -- prints a random number takes an number agruement ()
print(math.random(10, 100)) -- prints random values between 10-100
print(math.min(10,15,20,50,100)) --arguement of a list prints out the smallest number in the list will print 10 
print(math.max(500,1000,5000,10000)) -- arguement of a list prints out the largest number in the list this will print 10k
print(math.floor(50.5)) -- arguement of a number rounds down
print(math.ceil(50.8)) -- arguement of a number rounds up
print(math.ceil(50.5,10,10.8)) -- can have mutiple numbers
print(math.floor (10.5,11.5,12)) -- can have mutiple numbers
-- more math after this

----------------------------------------------------------------------------------------
-- #5 Strings

local String = "This is a string"
print(String)
local Number = "25" -- this is a string not a number but no problem we can convert it :)
local text = "Text"
print(type(String)) -- checking type - returns string
-- you can also convert a string to number
print(Number) -- this is a string
print(tonumber(Number)) -- this is now a Number
print(type(Number)) -- this is now a number
print(type(text)) -- this will print a string 
-- But if we convert this to a number we will get nil no value because we have not set a value
print(tonumber(text)) -- prints nil because there is no value assigned

-- Multi line string
local Mutiline_Str = [[This
is
a
Multi
line
string]]
print(Mutiline_Str) -- prints the whole string!!!

-- You can also do this with numbers you can do this with just about anydata type lol

local Multiline_num = [[ 55
20
15
23
89
96
54
546
67

676
546

]]
print(Multiline_num) -- prints all the numbers in this multiline!!!


-- Getting string length
print(#Mutiline_Str) -- returns the length of a string the # is just for length
-- also works for numbers
print(#Multiline_num) -- returns the length of numbers
-- coverting numbers to Strings
local c = tostring(55) -- converts to string
local d = tostring(21) -- converts these numbers to strings
print(type(c),type(d)) -- prints string because the numbers have been converted to that

----------------------------------------------------------------------------------------
-- Creating escape characters
--[[
\n - new line
\ - space
\t - Tab
\v - verticle Tab
\\ - Backslash
\" Double qoute
\'' single qoute
-- more escape characters after this]]

-- Coverting strings to lower case and uppercase

local Upper_case = "THIS IS A UPPERCASE STRING THAT NEEDS TO BE CONVERED TO LOWER"
local Lower_Case = "this is a lowercase string that needs to be converted to uppercase"
print(string.lower(Upper_case)) -- converts the upper case string to lower
print(string.upper(Lower_Case)) -- converts the lowercase string to upper
print(string.len(Upper_case)) -- gets length of the string also works for numbers
print(string.len(Lower_Case)) -- gets the length of the string.
-- or
print(#Lower_Case) -- same thing
print(#Upper_case) 
-- Getting a string sub

print(string.sub(Lower_Case,1,5)) -- gets words of a string
-- Getting a String character
print(string.char(65)) -- goes off by number characters
-- converting characters into bytes
print(string.byte("This")) -- converts this string to bytes

-- repeating strings
print(string.rep(Lower_Case,5)) -- two arguments text to repeat and times to repeat

-- finding words in a string
print(string.find(Lower_Case,"is")) -- finds the word you want then returns number start and end

-- matching strings
print(string.match(Lower_Case, "is")) -- matches the string returns the string if found
-- replacing things in a string
print(string.gsub(Lower_Case,"o", "O")) -- replaces the words with the new string 
-- more strings later

----------------------------------------------------------------------------------------

-- # 6 if statements
--if true then do something
-- elseif do something else
-- else do this

-- comparison operations
--[[> Greater than
    < Less than
    >= Greater than equal to
    <= Less than equal to
    ~= Not equal to
    ]]

---- If statement Examples

--local Number = 5

--if Number = 5 then
    -- do something
--elseif
    --number ~= 5 then
        -- do something
    --elseif
        --number ~= 8 then
            -- do something -- you can have as many elseif statemets are you want
           -- else
                --if number >=8 then -- the else is the final thing it comes last
                -- do something
--end -- needed to end
-- and this key word is used in conditions to check 1 conditon and the other
-- and and or
-- and -- must both be true
--or - only 1 conditon needs to be true for the code to execute
-- not - this means false

--and they must both be true in order to run
--true and true execute
--true and false not execute
--false and true not execute 
--false and false not execute

-- or only one needs to be true in order to run
--true or true execute
--true or false execute
--false or true execute
--false or false not execute

-- not
--not true = false
--not false = true

----------------------------------------------------------------------------------
--# -- 7 -- loops

----------------------------------------------------------------------------------
-- for loop 
-- for i,v in pairs do() - that's a tables for  loop i stands for index and v stands for value
-- the regular for loop is just for i = v Do
--      S  E  I    these are SEI start end and increase
--for i = 1,10,1 do -- Starting from 1, ending at 10, and increasing by +1
   -- print(i)
--end
--print(i) -- you can not print this out if it is local scope must be inside the for loop

-- reversing a for loop to do that we use a - negative
--       Start,End,Increase
for a = 1,10,-1 do
    print(a)
end
-- you can loop through arrays,tables etc.



----------------------------------------------------------------------------------

-- While loops

local peeps = 10

while peeps >0 do
    peeps = peeps - 1 -- stop the while loop like a wait()
    print(" There is " .. peeps .. "Left at the party") -- once this ends the loop breaks 
end
-- infinite loop will run forever
--while true do 
    --print("Infinite loop")
--end
-- you can create alot of applications with a while loop

-- Repeat loop

local x = 1 

repeat 
    print("In the repeat loop!") -- prints this while in loop your code goes here
    x = x + 1
until 

x>10 -- repeating until x = 10

-- the difference between a repeat loop and a while loop is that a repeat loop will run the code at least once no matter what

-- Creating a infinite repeat loop

--repeat 
--print("in a infinite repeat loop :0")
--until false -- it's infinite because it checks until the condion is true
----------------------------------------------------------------------------------
--#8 User Input
print("Hello There!")
local Input = io.read()
--io.read() -- allows you to get user Input
-- io.write -- the script will write it
print(Input)

----------------------------------------------------------------------------------
-- User input Examples
--local Correct = Num1 + Num2
--local Num1,Num2 = 10,5
--io.write("Input " .. Num1 .. " + ".. Num2 ..": ")

--local Anwser = io.read()

--if tonumber(Anwser) then
    --print("You are Right")
    --else
        --print("You Anwser incorrect")
  ----------------------------------------------------------------------------------