Multiple Player Detecting Parts

Hello! I was just curious if there are any ways to detect if the player is touching one of the 3 parts that are in different locations. So for example;

One part is in a group named A, the second one is in a group named B and the other one is in C. If the player touches one of these parts, an event would occur.

I already know how to detect multiple parts that are in the SAME location (using the code given below) in the workspace, but I’m unsure on how to detect the ones that are separated from each other.

for _, part in ipairs(Walls) do -- Walls being the folder that contains the parts that the player is going to touch.
	if part:IsA("BasePart") then
		part.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
                -- The code would be here.
		    end)
	    end
    end

You could just table.insert the objects you want into a table, and then iterate through the table instead of just the one workspace folder.

local basepartTable = {}

table.insert(basepartTable, workspace.part)

for i, v in pairs(basepartTable) do
	if v:IsA("BasePart") then
		part.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
                -- The code would be here.
		    end)
	    end
    end

So do I just need to write down the names of the parts in the table.insert brackets and if the player touches any of those parts the event would occur?

Only one parameter can be passed through table.insert at a time, so you’d just use table.insert once per object, or embed table.insert into a GetChildren() loop.

table.insert(table1, workspace.object1)
table.insert(table1, workspace.object2)
table.insert(table1, workspace.object3)
for i,v in pairs(workspace:GetChildren()) do
   if v:IsA("Part") then
      table.insert(table1, v)
   end
end

Yes, when the player touches any of the parts the event would occur.

Ah I see. I will test it right now so I can report the result to you.

It gave me this error any way to fix it?

invalid argument #1 to 'insert' (table expected, got Instance)

My bad I accidently flipped the parameters lol, it should actually be

table.insert(table1, object1)

Oh I see, that’s fine lol. Alright seems to have worked for now thank you for your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.