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

Is it possible for a table[all]?

For example:

local Table = {part1,part2}

Table[all].Touched:Connect(function()
	
end)

1 answer

There is a function called pairs, it's a function that loops through a table and returns it's index (Position or key in the table) and value. Since the name of the function makes no sense, it's hard to understand but here is example of how it works:

local Table = {
	'kirda',
	['cat'] = 50,
	[50] = 'string',
	'any',
	Instance.new('Part'),
}

for Index, Value in pairs(Table) do
	print(Index, Value)
end

> Output:
1 "kirda"
"cat" 50
50 "string"
2 "any"
3 Part (Instance)

There is also a similar function called ipairs which is same as pairs but is faster because it has exceptions, it breaks if Index is not number or if Value is nil so for example:

local table = {
	'kirda',
	'cat',
	['key'] = 24,
}

for Index, Value in ipairs(table) do
	print(Index, Value)
end

> Output:
1 "kirda"
2 "cat"
-- here it broke the loop because "key" is not a number, it's a string

Another example of the function when the Value is nil:

local table = {
	'kirda',
	'cat',
	nil,
	'some',
}

for Index, Value in ipairs(table) do
	print(Index, Value)
end

> Output:
1 "kirda"
2 "cat"
-- breaks because value is nil

So in your case, since you have table {part1, part2} all the indexes are numbers so ipairs is good for this, you need to loop through all the parts and connect them to Touched event, like this:

local Table = {part1,part2}

for _, Part in ipairs(Table) do
	Part.Touched:Connect(function()

	end)
end

2 comments

0

Yes I know about pairs(), but how do I do it at the same time, like without a loop?

0

it does it at the same time, loop is very fast. And you cant do that without a loop.

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