Advertisement
Guest User

loops and functions r080l0x

a guest
Dec 8th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.65 KB | None | 0 0
  1. -- hell0 i am sergeant and I will teach y00 scripting
  2. -----------------------------------------------------
  3.  
  4. print("Hello world!") -- prints "Hello World" in the output
  5.  
  6. --[[
  7.     when u make a script u get that ^^
  8.     put whateva u want in between those speech marks and its prints text
  9.    
  10.     .. what if I wanted to print that 10 times? I could do this:
  11. --]]
  12.  
  13. print("Hello wolrd!")
  14. print("Hello wolrd!")
  15. print("Hello wolrd!")
  16. print("Hello wolrd!")
  17. print("Hello wolrd!")
  18. print("Hello wolrd!")
  19. print("Hello wolrd!")
  20. print("Hello wolrd!")
  21. print("Hello wolrd!")
  22. print("Hello wolrd!")
  23.  
  24. -- but that looks real ugly. just a big copy and paste job. what if I made a mistake typing Hello? aw crap, I just did! now I have to go back and fix it all!...
  25. -- to make it easier, I can write a loop that runs 10 times printing hello world. like this:
  26.  
  27. -- FOR loop
  28. for i = 1, 10 do            -- "for how many times, do this". it's gonna run 10 times
  29.     print("Hello world")    -- "hello world" in output
  30. end                         -- end the loop code here.
  31.  
  32. -- this creates a variable called i, sets it to 1, and the "for ... do" loop adds 1 to i every time it goes through the loop code, until it reaches 10, and then it stops.
  33. -- TL;DR: code runs 10 times.
  34. -- so much easier to manage, right? ok, here's another way of doing it!
  35.  
  36. -- WHILE loop
  37. local i = 1                 -- create a variable called i and set it to 1.
  38. while i <= 10 do            -- "while i is less than or equal to 10, do this"
  39.     print("Hello world!")   -- print line
  40.     i = i + 1               -- add 1 to i
  41. end                         -- end the loop
  42.  
  43. -- the i <= 10 inside the "while ... do" part is what you call a parameter.
  44.     -- so let's say i = 1. that means i <= 10 is TRUE. because of that, the while loop continues, until it's false. "while this is true, keep doing this."
  45.     -- in the future, u can use this to all sortsa things... e.g. while playerNotRespawned do fart_on_dead_body() end
  46.     -- this is also why u might see in some scripts: while true do ... end. that's a neverending loop.
  47.  
  48. -- you might notice that the code kinda reads like english, doesn't it?
  49. -- the best code is the easiest to read.
  50. -- Here's another example:
  51.  
  52. -- REPEAT loop
  53. local i = 1                 -- make i. make it equal to 1
  54. repeat                      -- "repeat this code..."
  55.     print("Hello world!")   -- code
  56. until i == 10               -- "... until i is equal to 10"
  57.  
  58. -- i == 10 is a parameter. this is like the WHILE loop, but the loop stops when the parameter is TRUE.
  59. -- it maeks sense when u read it like its english.
  60.  
  61.  
  62.  
  63.  
  64. -- let's say I've got a sick script going on. I need it, for some reason, to print "Hello World!" 10 times when a certain thing happens!
  65. -- does that mean I have to literally copy and paste the same loop again and again all over the place?
  66. -- NO. you use a function. then you call it. it's basically like a portal to a section of code to run.
  67.    
  68. function hello()                -- create a function called hello()
  69.     for i = 1, 10 do            -- your lovely code.
  70.         print("Hello World!")
  71.     end                        
  72. end                             -- end function code
  73.  
  74. hello()                         -- runs the code inside the hello() function
  75.    
  76.  
  77. --[[
  78.     you mighta noticed that I put spaces for some lines of code to the left. those are called indents. they are there to help tell you where some code starts and ends.
  79.    
  80. function hello()
  81.  |  for i = 1, 10 do
  82.  |   |  print("Hello World!")
  83.  |   v  
  84.  v  end
  85. end
  86.  
  87. --]]
  88.    
  89. -- and when I type hello(), it runs whatever was inside the function, which is that loopcode which prints 10 times!
  90.  
  91.  
  92. -- ... soo uh, what are the brackets for?
  93. -- those are for creating local variables inside the function. i left it blank cuz I didn't need to do anything. you can too if you dont need any.
  94.     -- but what if I wanted to change the number of times it prints "Hello World"?
  95.    
  96. function hello(loopnumber)      -- a new local variable for the function, set when u call the function!
  97.     for i = 1, loopnumber do    -- the for statement runs however many times loopnumber is
  98.         print("Hello World!")
  99.     end                        
  100. end                            
  101.  
  102. hello(69)                           -- now I can set the local variable for the function, right here! this will print "Hello World" 69 times!
  103.    
  104. -- ...what if I wanted to change what is printed?
  105. -- you can make multiple local variables!
  106.  
  107.  
  108. function hello(loopnumber, text)        -- 2 new local variables!
  109.     for i = 1, loopnumber do   
  110.         print(text)                     -- prints the text u set
  111.     end                        
  112. end                            
  113.  
  114. hello(69, "420 M8")                     -- nice. don't forget to set the variables in the order u set them too.  loopnumber, text = 69, "420 M8"  in THAT order
  115.  
  116.  
  117.  
  118. -- of course, this is all basic code. just printing text. but you can use loops and functions for making things like several parts and other cool stuff quickly and efficiently!
  119.  
  120. -- if yo0 want mOAR just ask - serg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement