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

?Can someone help explain these things:

Asked by 9 years ago

I never got how people used ipairs, pairs, or #. It's like... I just don't get it.

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

ipairs and pairs are simply built in functions that iterate through arrays/tables. They are there for convenience so you don't have to write your own. These are used in generic for loops.

Usage example:

local array = {
    [index] = value,
    [2] = "b",
    ["Apple"] = "Banana"
}

for index, value in ipairs(array) do
    print(index, value)
end

--[[Output
    index output
    2 b
    Apple Banana
--]]

# is used to retrieve the number of items in an array/table. These are commonly used in basic for loops.

Usage Example:

local array = {1, "Apple", game.Workspace}

for i = 1, #array do
    print(array[i])
end

--[[Output
    1
    Apple
    Workspace
--]]
1
In your second example the output would actually show "1, Apple, Workspace" For more on pairs versus ipairs, https://scriptinghelpers.org/questions/4/whats-the-difference-between-pairs-and-ipairs Perci1 4988 — 9y
0
Now how would you use those to make gui's and stuff? Ethan_Waike 156 — 9y
0
Whoops, thanks Perci1! BlackJPI 2658 — 9y
Ad

Answer this question