I’m not understanding key words and some basic scripting stuff. I’ll put them all below. Please, if you can, answer what they mean and do and how they’re useful as well as how they can be used in games as an example. Thanks!
1 | keyword 1 = pairs |
2 | keyword 2 = ipairs |
3 | keyword 3 = next |
4 | - - [[for loops (numeric and generic as well as _, and i,v and such also explain the syntax?) |
5 | return (why not print?) |
6 | debounces |
7 | parameters and arguments (why do some functions have hit as a parameter and a, b or some kind of input variables? Don’t they all have to be similar? Also why do people use func or ... as a parameter?) - -]] |
This answer is meant to add to H4X0MSYT's (who did a good job summarizing).
pairs
is best used on a table that has both non-integer keys and values, whereas ipairs
should only be used for tables that have integer keys (ie those that you can treat like an array). ex:
01 | local myTable = { |
02 | RandomNumber = 5 , |
03 | Greeting = "Hi" , |
04 | AnotherTable = { } , |
05 | } |
06 | for k, v in pairs (myTable) do |
07 | print (k, v) |
08 | end |
09 |
10 | local list = { 5 , "Hi" , { } } |
11 | for i, v in ipairs (list) do |
12 | print (i, v) |
13 | end |
Note that pairs
will get everything that ipairs
does, but not the reverse. Also, ipairs
is essentially the same as doing this:
1 | local list = { 5 , "Hi" , { } } |
2 | for i = 1 , #list do |
3 | print (i, list [ i ] ) |
4 | end |
You really don't need to know about next
when you're using pairs/ipairs.
You can read more information on for
loops/their syntax on the lua manual.
return
has two uses: return early (ex if a condition isn't met) and/or return a value. An example of a function returning a value would be tostring
, which "return"s the desired string. As for returning early, an example: H4X0MSYT's debounce code can be written:
1 | local debounce = false |
2 |
3 | function Main() |
4 | if debounce then return end -- return early because we don't want to do anything if we're already doing something in Main |
5 | debounce = true |
6 | -- Code here |
7 | debounce = false |
8 | end |
A common case for using debounce is when you're listening to a Touch
event and have a wait.
Parameters and Arguments:
1 | function Example(a, b) -- 'a' and 'b' are parameters. You can think of 'a' as the name of the parameter. |
2 | return a + b |
3 | end |
4 | local c = 5 |
5 | print (Example(c, 2 )) -- 'c' and '2' are arguments. ex, 'c' is being passed in for the parameter 'a'. Also, when Example is running, it's "b" parameter will have the number 2. |
You can pass anything into a function, including other functions (used to add functionality to a function -- the Roblox wiki shows how to do this with debounce). The ...
is called "varargs" and represents "any number of arguments". You can use it like this:
01 | function Sum(...) |
02 | local args = { ... } -- capture the '...' into something usable |
03 | local sum = 0 |
04 | for i = 1 , #args do |
05 | sum = sum + args [ i ] |
06 | end |
07 | return sum |
08 | end |
09 | print (Sum()) -- 0 |
10 | print (Sum( 1 )) -- 1 |
11 | print (Sum( 1 , 2 , 3 , 4 )) -- 10 |
12 | print (Sum( 1 , "Hello" )) -- this will error when Sum attempts to add "Hello", but lua is okay with passing any type of value(s) into the vararg. |
[Edit - Follow up to comment]
[]
: This indexes a table -- that is, retrieves the "value" associated with a specific "key". ex:
01 | local t = { } -- The {} creates a new table |
02 | t [ "SomeKey" ] = "SomeValue" -- can also be written: t.SomeKey = "SomeValue" |
03 | --Once we've done that, we can retrieve the value by using the key: |
04 | print (t.SomeKey) -- will print out "SomeValue". Instead of printing it, we could assign it to a variable, pass it to a function, or whatever else we want to do with it. |
05 |
06 | --Note that you can use *anything* as a key or value except "nil". ex: |
07 | t [ Vector 3. new( 1 , 2 , 3 ) ] = 3 |
08 |
09 | --It is illegal to use "nil" as a key. If you use "nil" as a value, you remove that key from the table. ex: |
10 | t.SomeKey = nil -- now 'pairs' won't show you this key anymore unless you re-add it to the table |
11 |
12 | --The point of pairs is to go over every key and value in the table, in any order. |
13 | --ipairs goes over every *integer* key (ex 1, 2, 3, etc) and value, in numeric order. Note that if you have a gap in the table, ipairs might not print it. ex: |
14 |
15 | local t = { "a" , "b" } |
Regarding your debounce question: if debounce then return end
, to be clear, is just this:
1 | if debounce then |
2 | return |
3 | end |
4 | --rest of function here |
You appear to be asking why the "return" is necessary? The "end" on line 3 only means "this spot marks the end of the if
block", not the end of the function. An "end" doesn't mean "end what you're doing", just "the end of the most recently started block". ex:
1 | function Something() |
2 | if someCondition then |
3 | elseif otherCondition then |
4 | end -- this "end"s the 'if'. This must be done before you end the function. |
5 | end -- this "end"s the function. |
So, without the return
in the debounce code, lua would keep executing the rest of the function regardless of whether debounce
was true or not.
Another thing I didn't mention before is that return
can return as many values as you like:
1 | function ReturnTwoThings() |
2 | return 1 , 2 |
3 | end |
4 | local a, b = ReturnTwoThings() |
5 | print (a + b) -- 3 |
Do note that it's usually easiest if you don't return too many separate values - most common is to just return 0-1 values, though 2 isn't unusual either.
pairs: Part of loops. Used for non tables. ipairs: Another part of loops. Used for tables. next: You guessed it, more loops! for loops: It's saying for everything this, run this. Example:
1 | for i, v in pairs (game.Workspace:GetChildren()) do -- For everything in workspace, |
2 | if v.Name = = "Baseplate" then -- Run this code. |
3 | print ( "WooHoo!" ) |
4 | return |
5 | end -- |
6 | end |
return: halts the functon and returns the value. Example:
1 | function ReturnParent(Arg 1 ) |
2 | return Arg 1. Parent |
3 | end |
Although that function is useless for finding a parent and just stupid. debounce: It's a way of making a script not run again while it's allready running. Example:
1 | local Debounce = false |
2 |
3 | function Main() |
4 | if Debounce = = false then |
5 | Debounce = true |
6 | -- Code here |
7 | Debounce = false |
8 | end |
9 | end |
Now we go to args and params. Lets say I to tostring() which converts a number to string. What does it convert? We give it an arg to tell it what to do. Also the things like hit, you haven't been looking at cracked games have you? a,b etc represent an input needed. IDE's specify things like (STRING text, BOOL retry, NUMBER retries) to tell us what kind of value we give it. If it's incorrect, it will throw an error. Buttttt as everyone else said, Read the wiki. EDIT: We use functions in args just to make life alot easier.
1 | game.Players.PlayerAdded:connect( function (Plr) |
2 |
3 | end |
will do the same thing as
1 | function PlrAdder(Plr) |
2 |
3 | end |
4 | game.Players.PlayerAdded:connect(PlrAdded) |
We also use functions in certain things like DataStore UpdateAsync(). We give it a function as an arg because it's easier than making a function somewhere else to do it.