How can I find how many walls are between two points?

Pretty much what the title says, I’m looking for a way to tell how many walls are between two points.
Some Diagrams are below, if anyone can point me in the right direction that would be great, thanks.

I had some idea about Raycasting but its kinda foggy and I’m not sure how I would write the code for it.


I would say you continue to raycast to the point until you find no walls. Ex:

local point1 =,0,0)
local point2 =,10,0)

function goToPoint(point1, point2, wallnum)
    if not wallnum then wallnum = 0 end
    local ray = workspace:Raycast(point1, (point2-point1).Unit*(point2-point1).Magnitude)
    if ray and ray.Instance then
        return goToPoint(ray.Position, point2, wallnum+1)
    return wallnum

print(goToPoint(point1, point2))

Not too sure if this will work, I haven’t tested, but I think it will.

1 Like

Seems like a hacky solution but would work. Although, I found a much more comfortable solution that seemed to have worked after a little digging. Thank you for your answer though.

function findAllPartsOnRay(ray)
	local targets = {}
		local target = game.Workspace:FindPartOnRayWithIgnoreList(ray, targets)
		if target then
			table.insert(targets, target)
	until not target
	return targets
1 Like

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