Advertisement
JumpYScriptsz

Lua Tutorial

Oct 1st, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.63 KB | Help | 0 0
  1. --[[
  2.  
  3. Hello! JumpYScriptsz here, I will try to explain the basics of how to learn scripting in (ROBLOX) Lua. Enjoy!
  4.  
  5. =====================================
  6. ----------\\ >> INDEX << //----------
  7. =====================================
  8.  
  9. ========== >> BEGINNER << ==========
  10.  
  11. 1       Printing            = Line 31
  12. 2.1     Single Variables    = Line 49
  13. 2.2     Multiple Variables  = Line 60
  14. 3       Operations          = Line 159
  15.  
  16. ======== >> INTERMEDIATE << ========
  17.  
  18. 3.1     Further Operations  = Line 173
  19. 4       tables + functions  = Line 68
  20. 5       iterations (loops)  = Line 185
  21. 6       conditions          = Line 209
  22.  
  23. ======= >> MISCELLANIOUS << ========
  24.  
  25. 7       Value Types         = Line 150
  26. 8       The End Read        = Line 263
  27.  
  28. ]]
  29.  
  30.  
  31. --------------------------------------   { PRINTING }   --------------------------------------
  32.  
  33. print("hello world") -- outputs --> hello world
  34. -- You can print multiple lines with [[]]
  35. print([[
  36. hello
  37. world
  38. ]])
  39.  
  40. -- you can also print variables
  41. -- A variable is a name that can hold a value
  42. -- there are 2 types of variables: local and global
  43.  
  44. local i = 1 --> local (cannot be called outside a scope)
  45. a = 1       --> global (can be used throuhout the script)
  46.  
  47. print(a)    --> 1
  48.  
  49. ------------------------------------ { SINGLE VARIABLES } ------------------------------------
  50.  
  51. -- there are 4 values you can assign to a variable: nil, bool, numbers and strings
  52.  
  53. a = 1       --> number
  54. b = "Nice"  --> string (anything inside quotes)
  55. c = nil     --> no value, nothing
  56. d = true    --> boolean (can be either true or false)
  57.  
  58. print(a,b,c,d) --> you can print multiple variables in one line --> 1 Nice nil true
  59.  
  60. ------------------------------------ { MULTIPLE VARIABLES } ------------------------------------
  61.  
  62. local a,b,c,d = "hi",true,-1,nil    --> you can assign multiple values to multiple variables on one line
  63.  
  64. print(a,b,"some text: "..c,d)       --> hi true some text: -1 nil
  65.  
  66. -- , = spaces .. = concat (join together) e.g. "hi".."there" = "hithere"
  67.  
  68. ------------------------------------ { TABLES AND FUNCTIONS } ------------------------------------
  69.  
  70. t = {} -- empty table
  71.  
  72. my_table = {somestring="test", 1, true, thingy=false, "BeepBoop"} -- you can assign any value/s or variables to a table
  73.  
  74. -- to add an object to a table we use table.incert(TABLE_NAME, VALUE)
  75.  
  76. table.insert(t,"wassup!") -- incerts the values into my_table
  77.  
  78. -- to referance (locate) an object inside a table you put: table name, dot, and the variable name
  79.  
  80. print(my_table.thingy)  --> false
  81.  
  82. -- alternatively you can referance the object position in the table by using []
  83.  
  84. print(my_table[3])      --> BeepBoop
  85.  
  86. -- [3] represents the third value in the list.
  87. -- A position with a higher value than the number of items in a table returns nil (as it does not exist)
  88.  
  89. -- to loop through all values inside a table we use ipairs where:
  90.  
  91. --> the first variable is the number
  92. --> the second variable is the name
  93.  
  94. for i,v in ipairs(my_table) do -- cycles through values in the table "my_table" with the variables i (number) and v (name)
  95.  
  96.     print("Value is",v,"in place",i) -- outputs the following:
  97.    
  98.     --> Value is 1 in place 1
  99.     --> Value is true in place 2
  100.     --> Value is BeepBoop in place 3
  101.     --> Value is wassup! in place 4
  102. end
  103.  
  104. -- functions are things we call later on in a script
  105. -- we could use local functions (private) or global functions
  106. -- functions can pass params (values) into it
  107. -- example: adding function
  108.  
  109. i = 3
  110. e = 6
  111.  
  112. function add()
  113.     local sum = i + e -- sum can only be called within this function
  114.     print(sum)
  115. end
  116.  
  117. -- print(sum) --> sum not defined
  118.  
  119. -- declaring variables in functions we use local
  120. -- this is to make sure a variable with the same name in another place does not override it
  121. -- to call the function we say the name of it followed by 2 brackets (params)
  122.  
  123. add() --> 9
  124.  
  125. -- to pass params (values) to our function we use name values
  126. -- these value names can be anything
  127.  
  128. function multiply(a,b) -- a and b for example
  129.     local sum = a*b -- * = times
  130.     print(sum)
  131. end
  132.  
  133. -- this function takes 2 params, multiply them and output the answer
  134.  
  135.  
  136. multiply(4,3) --> 12
  137.  
  138.  
  139. -- we seperate our input values with commas
  140.  
  141. -- if there is a list of variables you want to save, we use ... (a table which can hold an inf amount of data)
  142.  
  143. function my_func(thing, ...)
  144.     print("I am going to see a",thing,"with", ...) --> I am going to see a movie with Charly Zak Fred James
  145. end
  146.  
  147. my_func("movie","Charly","Zak","Fred","James")
  148.  
  149.  
  150. ------------------------------------ { CHECK VALUE TYPE } -------------------------------------
  151.  
  152. -- you can use type() to check what type the value is
  153.  
  154. print(type("Hello world"))  --> string
  155. print(type(10.4*3))         --> number
  156. print(type(true))           --> boolean
  157. print(type(nil))            --> nil
  158.  
  159. ---------------------------------------- { OPERATIONS } ---------------------------------------
  160.  
  161. -- there are 4 main types of operations you can use: + - * /
  162.  
  163. add = 1+5  -- 1 plus 5
  164. sub = 9-4  -- 9 minus 4
  165. div = 12/4 -- 12 divided by 4
  166. pow = 10*2 -- 10 times 2
  167.  
  168. print(add) --> 6
  169. print(sub) --> 5
  170. print(div) --> 3
  171. print(pow) --> 20
  172.  
  173. ------------------------------------ { FURTHER OPERATIONS } -----------------------------------
  174.  
  175. -- there is also a built in math function which you can do other operations
  176.  
  177. print(math.random(1,10))    --> random number between 1 and 10
  178. print(math.huge)            --> inf
  179. print(math.round(14.3))     --> 14 (round numbers up or down)
  180.  
  181. a = (10/2)+4
  182.  
  183. print(math.sqrt(a))         --> 3 (you can even pass operations as variable form)
  184.  
  185. ---------------------------------------- { ITERATIONS } ---------------------------------------
  186.  
  187. -- iterations (loops) allows us to repeat
  188. -- try avoid while true do with no wait() as it lags
  189.  
  190. while wait(.1) do   --> this will run forever every 0.1 seconds
  191.     break           --> allows us to stop the forever loop
  192. end
  193.  
  194. -- we can also set conditions to our loops so they can stop
  195.  
  196. for i = 0,5 do  --> counts from i = 0 to i = 5 then stops
  197.     print(i)    --> 0 1 2 3 4 5
  198. end
  199.  
  200. -- repeat untils are useful to
  201. -- use 2 equal signs to check if a condition matches another condition / value
  202.  
  203. e=0
  204.  
  205. repeat e += 1   --  you could use e=e+1 but this is shorter (add one, save value, add one to that saved value and so on)
  206.     print(e)    --> 1 2 3 4 5 6 7 8 9 10 11 12
  207. until e == 12
  208.  
  209. --------------------------------------- { CONDITIONS } -----------------------------------------
  210.  
  211. -- conditions can be represented as many things. Some being: true, false, ==, ~= (not equal to), or, and
  212.  
  213. value = true
  214.  
  215. if false then -- false is a constant along with true and nil (does not change)
  216.     print("this will not print")
  217. end
  218.  
  219. if value == true then
  220.     print("value is true!")
  221. end
  222.  
  223. -- we can shorten this down
  224.  
  225. if value then
  226.     print("value is true!")
  227. end
  228.  
  229. -- alternatively we can use "not"
  230.  
  231. if not value then -- if not true then...
  232.     print("value is false")
  233. end
  234.  
  235. -- we can check our value by merging both of these together as one statement
  236.  
  237. t = false
  238.  
  239. if value then
  240.     print("value is true!")
  241. else
  242.     print("value is false!")
  243. end
  244.  
  245. -- we can also embed mutliple if statements into one
  246.  
  247. test = "hello person" -- change this value type to whatever to test it!
  248.  
  249. if type(test) == "boolean" then -- check if the value is a boolean
  250.     if test then
  251.         print("test is true!")
  252.         -- alternatively can be written as...
  253.         print("test is",test.."!")
  254.     else
  255.         print("test is false!")
  256.     end
  257. elseif type(test) == "string" then   --> we use elseif in one if statement to check for supposed multiple outcomes
  258.     print("value test is a string!") --> this will be printed
  259. else -- if no conditions are met
  260.     print("Value test is not a boolean or a string! It is a",type(test).."!!!")
  261. end
  262.  
  263. -- I hope you have learned some things about lua and how to script! - JS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement