Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

?Can someone help explain these things:

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

1 answer

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
--]]

3 comments

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

0

Now how would you use those to make gui's and stuff?

0

Whoops, thanks Perci1!

This discussion is preserved as it was. New answers, comments, and votes are closed.