Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- hell0 i am sergeant and I will teach y00 scripting
- -----------------------------------------------------
- print("Hello world!") -- prints "Hello World" in the output
- --[[
- when u make a script u get that ^^
- put whateva u want in between those speech marks and its prints text
- .. what if I wanted to print that 10 times? I could do this:
- --]]
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- print("Hello wolrd!")
- -- 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!...
- -- to make it easier, I can write a loop that runs 10 times printing hello world. like this:
- -- FOR loop
- for i = 1, 10 do -- "for how many times, do this". it's gonna run 10 times
- print("Hello world") -- "hello world" in output
- end -- end the loop code here.
- -- 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.
- -- TL;DR: code runs 10 times.
- -- so much easier to manage, right? ok, here's another way of doing it!
- -- WHILE loop
- local i = 1 -- create a variable called i and set it to 1.
- while i <= 10 do -- "while i is less than or equal to 10, do this"
- print("Hello world!") -- print line
- i = i + 1 -- add 1 to i
- end -- end the loop
- -- the i <= 10 inside the "while ... do" part is what you call a parameter.
- -- 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."
- -- in the future, u can use this to all sortsa things... e.g. while playerNotRespawned do fart_on_dead_body() end
- -- this is also why u might see in some scripts: while true do ... end. that's a neverending loop.
- -- you might notice that the code kinda reads like english, doesn't it?
- -- the best code is the easiest to read.
- -- Here's another example:
- -- REPEAT loop
- local i = 1 -- make i. make it equal to 1
- repeat -- "repeat this code..."
- print("Hello world!") -- code
- until i == 10 -- "... until i is equal to 10"
- -- i == 10 is a parameter. this is like the WHILE loop, but the loop stops when the parameter is TRUE.
- -- it maeks sense when u read it like its english.
- -- 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!
- -- does that mean I have to literally copy and paste the same loop again and again all over the place?
- -- NO. you use a function. then you call it. it's basically like a portal to a section of code to run.
- function hello() -- create a function called hello()
- for i = 1, 10 do -- your lovely code.
- print("Hello World!")
- end
- end -- end function code
- hello() -- runs the code inside the hello() function
- --[[
- 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.
- function hello()
- | for i = 1, 10 do
- | | print("Hello World!")
- | v
- v end
- end
- --]]
- -- and when I type hello(), it runs whatever was inside the function, which is that loopcode which prints 10 times!
- -- ... soo uh, what are the brackets for?
- -- 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.
- -- but what if I wanted to change the number of times it prints "Hello World"?
- function hello(loopnumber) -- a new local variable for the function, set when u call the function!
- for i = 1, loopnumber do -- the for statement runs however many times loopnumber is
- print("Hello World!")
- end
- end
- hello(69) -- now I can set the local variable for the function, right here! this will print "Hello World" 69 times!
- -- ...what if I wanted to change what is printed?
- -- you can make multiple local variables!
- function hello(loopnumber, text) -- 2 new local variables!
- for i = 1, loopnumber do
- print(text) -- prints the text u set
- end
- end
- 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
- -- 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!
- -- if yo0 want mOAR just ask - serg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement