Storing a value from a loop [Solved]

local test = {
	["One"] = game.Workspace.Platform_1,
	["Two"] = game.Workspace.Platform_2,
	["Three"] = game.Workspace.Platform_3,
	["Four"] = game.Workspace.Platform_4
}

for i, CollidedPart in pairs(test) do
	CollidedPart.Touched:Connect(function(TouchedPart)
		print(CollidedPart.Name .. " Touched " .. TouchedPart.Name)
	end)
end

I have a table and a loop that looks for parts that collided with stored parts in the table. My main question is how I could store the part that collided with the parts in the table.

Insert the part into the table using:
table.insert(table, part)
(the index will automatically turn into a number using this)

Code
local test = {
	[1] = game.Workspace.Platform_1,
	[2] = game.Workspace.Platform_2,
	[3] = game.Workspace.Platform_3,
	[4] = game.Workspace.Platform_4
}

for i, CollidedPart in pairs(test) do
	CollidedPart.Touched:Connect(function(TouchedPart)
		print(CollidedPart.Name .. " Touched " .. TouchedPart.Name)
		if not table.find(test, TouchedPart) then
			table.insert(test, TouchedPart)
		end
	end)
end

(this code can only supports number index)


You can also place the part in a custom index (number or string):
table["Part_1"] = part

Code
local test = {
	["Part_1"] = game.Workspace.Platform_1,
	["Part_2"] = game.Workspace.Platform_2,
	["Part_3"] = game.Workspace.Platform_3,
	["Part_4"] = game.Workspace.Platform_4
}

local function dictionaryLength(dictionary)
	local length = 0
	for i,v in pairs(dictionary) do
		length += 1
	end
	return length
end

local function dictionaryFind(dictionary, value)
	for i,v in pairs(dictionary) do
		if v == value then
			return true
		end
	end
end


for i, CollidedPart in pairs(test) do
	CollidedPart.Touched:Connect(function(TouchedPart)
		print(CollidedPart.Name .. " Touched " .. TouchedPart.Name)
		local testLength = dictionaryLength(test)
		local customIndex = ("Part_%s"):format(testLength + 1)
		if test[customIndex] == nil then
			test[customIndex] = TouchedPart
		end 
	end)
end
1 Like

Oh thank you so much, This greatly helped me alot. Could you explain what this means I’m kinda new to tables and wondering how this works out.

local customIndex = ("Part_%s"):format(testLength + 1)

%s will be turned into what ever is passed in in the format:

So if testLength was 3 then testLength - 1 would be 2 which means:

Will be equal to "Part_2. Hope this helps.

P.S this has nothing to do with tables, this is string manipulation.

1 Like

Oh I see, yeah that makes alot of sense.

wait why are we using these two different functions, I have got no clue for their purpose

Well, dictionaries have string indices and the original functions (table.insert, table.find) of those two functions only support numbered indices.

ah gotcha, I really need to pick up a dictionary and read what half of those meant. But I thank you for solving this problem for me

Tables are both Arrays and Dictionaries, they can hold multiple datatypes.
They have an “index” and a “value”, when you do something like: table[index] it’ll give you the value of that index.

Arrays have ordered indexes starting from 1 to how ever many are added, in arrays you do not give the index; instead it gives the index itself.
An array will look like:

local Array = {3, 2, 1}

Loop through this and you’ll see how they’re ordered:

for index, value in pairs(Array) do
    print(string.format("%d | %d", index, value)) 
end
--[[
1 | 3
2 | 2
3 | 1
]]

In Dictionaries you give the index, they’re not ordered.
A Dictionary will look like:

local Dictionary = {
    Index1 = 3;
    Index2 = 2;
    Index3 = 1;
}

Loop through this and you’ll see the specified indexes:

for index, value in pairs(Dictionary) do
    print(string.format("%s | %d", index, value))
end
--[[
Index1 | 3
Index2 | 2
Index3 | 1
]]

Dictionaries can also have spaces and non-numeric/non-alphabetic characters in their name though I don’t recommend it, here’s how:

local Dictionary = {
    ["Hello world"] = true;
    ["I have spaces"] = true;
    ["hello?"] = true;
}
1 Like