Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Hello! JumpYScriptsz here, I will try to explain the basics of how to learn scripting in (ROBLOX) Lua. Enjoy!
- =====================================
- ----------\\ >> INDEX << //----------
- =====================================
- ========== >> BEGINNER << ==========
- 1 Printing = Line 31
- 2.1 Single Variables = Line 49
- 2.2 Multiple Variables = Line 60
- 3 Operations = Line 159
- ======== >> INTERMEDIATE << ========
- 3.1 Further Operations = Line 173
- 4 tables + functions = Line 68
- 5 iterations (loops) = Line 185
- 6 conditions = Line 209
- ======= >> MISCELLANIOUS << ========
- 7 Value Types = Line 150
- 8 The End Read = Line 263
- ]]
- -------------------------------------- { PRINTING } --------------------------------------
- print("hello world") -- outputs --> hello world
- -- You can print multiple lines with [[]]
- print([[
- hello
- world
- ]])
- -- you can also print variables
- -- A variable is a name that can hold a value
- -- there are 2 types of variables: local and global
- local i = 1 --> local (cannot be called outside a scope)
- a = 1 --> global (can be used throuhout the script)
- print(a) --> 1
- ------------------------------------ { SINGLE VARIABLES } ------------------------------------
- -- there are 4 values you can assign to a variable: nil, bool, numbers and strings
- a = 1 --> number
- b = "Nice" --> string (anything inside quotes)
- c = nil --> no value, nothing
- d = true --> boolean (can be either true or false)
- print(a,b,c,d) --> you can print multiple variables in one line --> 1 Nice nil true
- ------------------------------------ { MULTIPLE VARIABLES } ------------------------------------
- local a,b,c,d = "hi",true,-1,nil --> you can assign multiple values to multiple variables on one line
- print(a,b,"some text: "..c,d) --> hi true some text: -1 nil
- -- , = spaces .. = concat (join together) e.g. "hi".."there" = "hithere"
- ------------------------------------ { TABLES AND FUNCTIONS } ------------------------------------
- t = {} -- empty table
- my_table = {somestring="test", 1, true, thingy=false, "BeepBoop"} -- you can assign any value/s or variables to a table
- -- to add an object to a table we use table.incert(TABLE_NAME, VALUE)
- table.insert(t,"wassup!") -- incerts the values into my_table
- -- to referance (locate) an object inside a table you put: table name, dot, and the variable name
- print(my_table.thingy) --> false
- -- alternatively you can referance the object position in the table by using []
- print(my_table[3]) --> BeepBoop
- -- [3] represents the third value in the list.
- -- A position with a higher value than the number of items in a table returns nil (as it does not exist)
- -- to loop through all values inside a table we use ipairs where:
- --> the first variable is the number
- --> the second variable is the name
- for i,v in ipairs(my_table) do -- cycles through values in the table "my_table" with the variables i (number) and v (name)
- print("Value is",v,"in place",i) -- outputs the following:
- --> Value is 1 in place 1
- --> Value is true in place 2
- --> Value is BeepBoop in place 3
- --> Value is wassup! in place 4
- end
- -- functions are things we call later on in a script
- -- we could use local functions (private) or global functions
- -- functions can pass params (values) into it
- -- example: adding function
- i = 3
- e = 6
- function add()
- local sum = i + e -- sum can only be called within this function
- print(sum)
- end
- -- print(sum) --> sum not defined
- -- declaring variables in functions we use local
- -- this is to make sure a variable with the same name in another place does not override it
- -- to call the function we say the name of it followed by 2 brackets (params)
- add() --> 9
- -- to pass params (values) to our function we use name values
- -- these value names can be anything
- function multiply(a,b) -- a and b for example
- local sum = a*b -- * = times
- print(sum)
- end
- -- this function takes 2 params, multiply them and output the answer
- multiply(4,3) --> 12
- -- we seperate our input values with commas
- -- if there is a list of variables you want to save, we use ... (a table which can hold an inf amount of data)
- function my_func(thing, ...)
- print("I am going to see a",thing,"with", ...) --> I am going to see a movie with Charly Zak Fred James
- end
- my_func("movie","Charly","Zak","Fred","James")
- ------------------------------------ { CHECK VALUE TYPE } -------------------------------------
- -- you can use type() to check what type the value is
- print(type("Hello world")) --> string
- print(type(10.4*3)) --> number
- print(type(true)) --> boolean
- print(type(nil)) --> nil
- ---------------------------------------- { OPERATIONS } ---------------------------------------
- -- there are 4 main types of operations you can use: + - * /
- add = 1+5 -- 1 plus 5
- sub = 9-4 -- 9 minus 4
- div = 12/4 -- 12 divided by 4
- pow = 10*2 -- 10 times 2
- print(add) --> 6
- print(sub) --> 5
- print(div) --> 3
- print(pow) --> 20
- ------------------------------------ { FURTHER OPERATIONS } -----------------------------------
- -- there is also a built in math function which you can do other operations
- print(math.random(1,10)) --> random number between 1 and 10
- print(math.huge) --> inf
- print(math.round(14.3)) --> 14 (round numbers up or down)
- a = (10/2)+4
- print(math.sqrt(a)) --> 3 (you can even pass operations as variable form)
- ---------------------------------------- { ITERATIONS } ---------------------------------------
- -- iterations (loops) allows us to repeat
- -- try avoid while true do with no wait() as it lags
- while wait(.1) do --> this will run forever every 0.1 seconds
- break --> allows us to stop the forever loop
- end
- -- we can also set conditions to our loops so they can stop
- for i = 0,5 do --> counts from i = 0 to i = 5 then stops
- print(i) --> 0 1 2 3 4 5
- end
- -- repeat untils are useful to
- -- use 2 equal signs to check if a condition matches another condition / value
- e=0
- 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)
- print(e) --> 1 2 3 4 5 6 7 8 9 10 11 12
- until e == 12
- --------------------------------------- { CONDITIONS } -----------------------------------------
- -- conditions can be represented as many things. Some being: true, false, ==, ~= (not equal to), or, and
- value = true
- if false then -- false is a constant along with true and nil (does not change)
- print("this will not print")
- end
- if value == true then
- print("value is true!")
- end
- -- we can shorten this down
- if value then
- print("value is true!")
- end
- -- alternatively we can use "not"
- if not value then -- if not true then...
- print("value is false")
- end
- -- we can check our value by merging both of these together as one statement
- t = false
- if value then
- print("value is true!")
- else
- print("value is false!")
- end
- -- we can also embed mutliple if statements into one
- test = "hello person" -- change this value type to whatever to test it!
- if type(test) == "boolean" then -- check if the value is a boolean
- if test then
- print("test is true!")
- -- alternatively can be written as...
- print("test is",test.."!")
- else
- print("test is false!")
- end
- elseif type(test) == "string" then --> we use elseif in one if statement to check for supposed multiple outcomes
- print("value test is a string!") --> this will be printed
- else -- if no conditions are met
- print("Value test is not a boolean or a string! It is a",type(test).."!!!")
- end
- -- I hope you have learned some things about lua and how to script! - JS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement