How can I check how many parts are touching a certain part?

Here I am again! This time, I have run into issues when trying the :GetTouchingParts() function.

What I am trying to do:

  • Check if any other vehicle is in a certain area
    If not:
  • Spawn a vehicle at a place

However, to be able to check the amount of parts touching, using the function provided above, you need to have CanCollide activated (which is bad, as else the vehicles might get flung) or a TouchInterest (which doesn’t exist unless a Touching function exists, which doesn’t in my spawn code).

Is there any alternative to that function to check how many parts are touching, or do I have to create a small “Touching” function to create a TouchInterest?

Checking the Wiki wasn’t really helpful.

1 Like

workspace:GetPartsInPart(ZonePart) or workspace:GetPartBoundsInBox(ZonePart.CFrame,ZonePart.Position).
They won’t work with touched but they should be useful if you’re checking if they occupy a space within a zoned area.

While hacky, you could connect an ‘empty’ lambda function to each of the vehicle’s parts ‘Touched’ signal.

local Workspace = workspace
local Vehicle = Workspace.Vehicle --Example reference to a vehicle model. 

for _, Descendant in ipairs(Vehicle:GetDescendants()) do
	if not (Descendant:IsA("BasePart")) then continue end
	Descendant.Touched:Connect(function() end)

The best option however would be to use ‘GetPartsInPart()’.

do #Part:GetTouchingParts()

Part:GetTouchingParts() returns a table with all parts touching the part if you put a # in front of it it will returns the how many items the list has

1 Like

The thing is, that will return 0 no matter how many parts are touching that certain part if the part’s CanCollide is false and the part doesn’t have TouchInterest.

GetTouchingParts() and .Touched aren’t working if CanCollide == false

Did you read the note at the top? :slightly_smiling_face: