Detect if there is a special part in array

How do i find a part with specific name if it is contained inside of an array?? I have tried to use table.find() and it gave me “nil” when there was a VehicleSeat inside of a cancollide false part

while true do
	print("checking spawn1")
	local alltouchedpartss = workspace:GetPartsInPart(Partt)
	print(table.find(alltouchedpartss, "VehicleSeat"))
	if table.find(alltouchedpartss, "VehicleSeat") then
		print("yes")
	end
	task.wait(5)
end

image

You’ll have to use a loop to check. One way is create another table that changes the object to the name, such that you can then use table.find on it to find the index.

while true do
	print("checking spawn1")
	local alltouchedpartss = workspace:GetPartsInPart(Partt)
    local alltouchedpartsss = {}
    for _, part in ipairs(allthouchedpartss) do
        table.insert(alltouchedpartsss, part.Name)
    end
	print(table.find(alltouchedpartsss, "VehicleSeat"))
	if table.find(alltouchedpartsss, "VehicleSeat") then
		print("yes")
	end
	task.wait(5)
end

Another method of doing it is to not use table.find at all, and instead just use a loop:

while true do
	print("checking spawn1")
	local alltouchedpartss = workspace:GetPartsInPart(Partt)
    for index, part in ipairs(alltouchedpartss) do
        if part.Name == "VehicleSeat" then -- or part:IsA("VehicleSeat")
            print("yes")
            break
        end
    end
	task.wait(5)
end
2 Likes

I’m pretty sure GetPartsInPart returns an array of objects and not strings. Try finding an instance instead in the table

for i, v in ipairs(alltouchedpartsss) do
   if v:IsA(“VehicleSeat”) then print(“yes”) end
end
1 Like

thank you for solution, it works perfectly.

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