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:
local myTable = {
RandomNumber = 5,
Greeting = "Hi",
AnotherTable = {},
}
for k, v in pairs(myTable) do
print(k, v)
end
local list = {5, "Hi", {}}
for i, v in ipairs(list) do
print(i, v)
end
Note that pairs will get everything that ipairs does, but not the reverse. Also, ipairs is essentially the same as doing this:
local list = {5, "Hi", {}}
for i = 1, #list do
print(i, list[i])
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:
local debounce = false
function Main()
if debounce then return end
debounce = true
debounce = false
end
A common case for using debounce is when you're listening to a Touch event and have a wait.
Parameters and Arguments:
function Example(a, b)
return a + b
end
local c = 5
print(Example(c, 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:
function Sum(...)
local args = {...}
local sum = 0
for i = 1, #args do
sum = sum + args[i]
end
return sum
end
print(Sum())
print(Sum(1))
print(Sum(1, 2, 3, 4))
print(Sum(1, "Hello"))
[Edit - Follow up to comment]
[]: This indexes a table -- that is, retrieves the "value" associated with a specific "key". ex:
local t = {}
t["SomeKey"] = "SomeValue"
print(t.SomeKey)
t[Vector3.new(1, 2, 3)] = 3
t.SomeKey = nil
local t = {"a", "b"}
t[4] = "d"
for i, v in ipairs(t) do
print(i, v)
end
Regarding your debounce question: if debounce then return end, to be clear, is just this:
if debounce then
return
end
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:
function Something()
if someCondition then
elseif otherCondition then
end
end
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:
function ReturnTwoThings()
return 1, 2
end
local a, b = ReturnTwoThings()
print(a + b)
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.
7 comments
There is something called variable names and such...
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.
I’m just showing them what do you want me to do put them in variables?
Oh ok
Please read the wiki in the future.
@lukeb50 I have I don’t understand it however
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" =)