Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need help understanding key words and simple basic scripting stuff help?

Asked by 7 years ago
Edited 7 years ago

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!

1keyword1 = pairs
2keyword2 = ipairs
3keyword 3 = next
4- - [[for loops (numeric and generic as well as _, and i,v and such also explain the syntax?)
5return (why not print?)
6debounces
7parameters 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?) - -]]
0
There is something called variable names and such... hiimgoodpack 2009 — 7y
0
Okay, so pairs loop through tables, ipairs wastes time checking unnecessary stuff, next is not useful, for loops can be found on youtube, return and print are two different things, debounces are like cooldowns, parameters are the info you send to functions, and arguments are the parameters you receive. hiimgoodpack 2009 — 7y
0
I’m just showing them what do you want me to do put them in variables? Subaqueously 11 — 7y
0
Oh ok Subaqueously 11 — 7y
View all comments (3 more)
0
Please read the wiki in the future. lukeb50 631 — 7y
0
@lukeb50 I have I don’t understand it however Subaqueously 11 — 7y
1
I believe this question is a good example of what *should* be on this site -- people asking what things mean, rather than just "my script isn't working, please help" =) chess123mate 5873 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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:

01local myTable = {
02    RandomNumber = 5,
03    Greeting = "Hi",
04    AnotherTable = {},
05}
06for k, v in pairs(myTable) do
07    print(k, v)
08end
09 
10local list = {5, "Hi", {}}
11for i, v in ipairs(list) do
12    print(i, v)
13end

Note that pairs will get everything that ipairs does, but not the reverse. Also, ipairs is essentially the same as doing this:

1local list = {5, "Hi", {}}
2for i = 1, #list do
3    print(i, list[i])
4end

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:

1local debounce = false
2 
3function 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
8end

A common case for using debounce is when you're listening to a Touch event and have a wait.

Parameters and Arguments:

1function Example(a, b) -- 'a' and 'b' are parameters. You can think of 'a' as the name of the parameter.
2    return a + b
3end
4local c = 5
5print(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:

01function 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
08end
09print(Sum()) -- 0
10print(Sum(1)) -- 1
11print(Sum(1, 2, 3, 4)) -- 10
12print(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:

01local t = {} -- The {} creates a new table
02t["SomeKey"] = "SomeValue" -- can also be written: t.SomeKey = "SomeValue"
03--Once we've done that, we can retrieve the value by using the key:
04print(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:
07t[Vector3.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:
10t.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 
15local t = {"a", "b"}
View all 30 lines...

Regarding your debounce question: if debounce then return end, to be clear, is just this:

1if debounce then
2    return
3end
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:

1function 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.
5end -- 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:

1function ReturnTwoThings()
2    return 1, 2
3end
4local a, b = ReturnTwoThings()
5print(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.

0
Wow thank you so much this has helped a lot I will surely reference to this and use it for the future. Also what is [] and why is it used. Could you also explain why at if debounce then return end why we return and not just end and what the difference makes Subaqueously 11 — 7y
Ad
Log in to vote
1
Answered by
H4X0MSYT 536 Moderation Voter
7 years ago
Edited 7 years ago

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:

1for 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                                                                   --
6end

return: halts the functon and returns the value. Example:

1function ReturnParent(Arg1)
2    return Arg1.Parent
3end

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:

1local Debounce = false
2 
3function Main()
4    if Debounce == false then
5        Debounce = true
6        -- Code here
7        Debounce = false
8    end
9end

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.

1game.Players.PlayerAdded:connect(function(Plr)
2 
3end

will do the same thing as

1function PlrAdder(Plr)
2 
3end
4game.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.

0
pairs is a iterator function ONLY for tables, not for non-tables. (how'd that work with non tables?) hiimgoodpack 2009 — 7y
0
What is arg1’s Parent Subaqueously 11 — 7y
0
Also when i said func in arguments I literally meant the word “func” Subaqueously 11 — 7y
0
thats just a name for a varible. H4X0MSYT 536 — 7y
0
Lmao accepts other answer and not mine gg no re. H4X0MSYT 536 — 7y

Answer this question