How to check if there's a part at a given position?

Hello,
I’m looking for the most efficient way to check if there is a part at a given position. I can’t use workspace:GetDescendants() to check the position of every part as that would slow the game down - I’m looking for something like workspace:Raycast(), except for a single Vector3 position instead of a Ray.
Thanks in advance.

1 Like

Do you mean, you just want to find the part’s location?

No, I need to check if a given position is being blocked by any part.

Can I please see your script???

I believe something using GetPartBoundsInBox would do the trick, passing in the Vector3 location you want to check (giving it as a CFrame however), chosing a small size for the box and optional OverlapParams. It’ll return you an array of parts that touched the bounds you specified, if the array is empty then no parts are touching it

3 Likes

Thanks, that’s exactly what I was looking for.

1 Like

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3

1 Like
function GetPartsAtPosition(Position :Vector3)
	local Touching = workspace:GetPartBoundsInBox(CFrame.new(Position), Vector3.one*0.1, OverlapParams.new())
	for i = 1, #Touching-1 do
		if Touching[i].CFrame.Position ~= Position then
			table.remove(Touching, i)
		end
	end
	return Touching or {} 
end

---------------------------------------------------

local PartsAtPosition = GetPartsAtPosition(Vector3.new(-540.25, -1.25, -8.75))
if PartsAtPosition >= 1 then
	print('Part detected!')
	print('Random part: ', PartsAtPosition[math.random(1, #PartsAtPosition)])
else
	print('No parts at that position')
end
2 Likes