Check for parts in a table

I need help in accessing a table of touchingParts and check if a part with a certain name is there. Im not familiar with this and I have only used touchConnect in my life. Can someone please help me review and tell me what im doing wrong.

local seat = script.Parent

while true do
	wait()
	local partTouching = workspace:GetPartsInPart(seat)
	print(partTouching)
	if partTouching.Name == "CertainPartName" then
		print("Working")
	end
end


1 Like

Try this:

local seat = script.Parent
local touchableParts = workspace:GetPartsInPart(seat)

for _,part in pairs(touchableParts) do
    part.Touched:Connect(function(touchedPart)
        if touchedPart:IsA("Part") then -- checking if its actually a part instead of using it's name.
            print("Working")
        end
    end)
end

It might not be what you want but I never worked with :GetPartsInPart

I was trying to check if there is a part in the table in which its name is called Part. Bc :GetPartsInPart returns a table and I dont know how to check if the table has a part of a certain name

Just add a check when looping, here’s a slightly edited version of @PlyDev’s code:

local seat = script.Parent
local touchableParts = workspace:GetPartsInPart(seat)

for _,part in pairs(touchableParts) do
	part.Touched:Connect(function(touchedPart)
		if touchedPart:IsA("Part") and touchedPart.Name == "PART NAME" then
			print("Found part!")
			
			--you can also break here if you don't want to search for more parts with the same name.
		end
	end)
end

What is the purpose of the part.Touched:Connect Function

The reason im using :GetpartsInPart is because my part is not capable of firing TouchEvent, due to both parts being anchored and non-collide

Oh oops I didn’t pay attention to that sorry, I just copied the code from PlyDev’s reply, all you need to do is loop through, no need to connect events.

local checkPart = script.Parent
local touchableParts = workspace:GetPartsInPart(checkPart)

for _,part in pairs(touchableParts) do
	if part:IsA("Part") and part.Name == "PART NAME" then
		print("Found part!")

		--you can also break if you don't want to search for more parts with the same name.
	end
end

This does not rely on touched events so it should work fine for your case.

Thank you so much for the effort. I really appreciate it

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