How do I get the distance between 2 players, if they're behind a wall? (without using .Magnitude)

Hello!
So, I know there’s a way to get the distance like mostly everyone does like this (p1 - p2).Magntiude. And it works fine in most cases. But I’m making a tracker tool and I need it to be able to get the distance of the part even if they’re behind a wall.

To give a better understanding, this is not how I need it to act:

This is how I need it to get the distance:

I don’t really know how to search this up anywhere, so if you find a possible solution from another solved post. Please make sure to link it.

And overall, any help is appreciated!

2 Likes

I don’t really have a clue on how to do this without using a service, maybe utlizing PathfindingService to find a path from one player to another and then adding up the magnitudes between the points would work?

3 Likes

That actually sounds clever, I’ll give it some tries

2 Likes

Worked flawlessly and simple!

This is the code for anyone wondering, if they do get on this post someday.

local PathfindingService = game:GetService("PathfindingService")

local path = PathfindingService:CreatePath()
path:ComputeAsync(workspace.Part1.Position, workspace.Part2.Position)

local waypoints = path:GetWaypoints()

local totalDistance = 0

local function getMagnitude(p1, p2)
	return (p1 - p2).Magnitude
end

for i, waypoint in pairs(waypoints) do
	local nextWP = waypoints[i + 1]
	if nextWP then
		totalDistance += getMagnitude(waypoint.Position, nextWP.Position)
	end
end

print(totalDistance)
1 Like

Why don’t you want to use the standard formula for distance?

I showed images referencing how I needed it to act.

I didn’t see that greenish line.

1 Like

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