How do you calculate the distance from your character to the top of a part?

  1. What do you want to achieve?
    I am making a ledge grab parkour game, and I want to know how I would go about making it so you can grab ledges, without having to name each part things such as “Ledge”.

  2. What is the issue?
    I need to know how I would go about calculating the distance from your character to the top of a part.

  3. What solutions have you tried so far?
    I tried finding solutions on the Developer Wiki but didn’t find much useful information about this topic. Thanks for the help.

2 Likes

To calculate part distance without naming every part name you need to use :GetChildren() inside of model or folder.
Here how to do it with infinite distance and without infinite distance.

local player = game.Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local Parts = workspace.Parts
while wait() do
	for _,v in pairs(workspace.Parts:GetChildren()) do
		local Dis = (character.PrimaryPart.Position - v.Position).Magnitude
		print(math.floor(Dis)..v.Name)
	end
end

But if you want to calculate distance if the player was close enough then do this.

local player = game.Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local Parts = workspace.Parts
local Away = 10
while wait() do
	for _,v in pairs(workspace.Parts:GetChildren()) do
		local Dis = (character.PrimaryPart.Position - v.Position).Magnitude
	    if Dis <= Away then
        print(math.floor(Dis)..v.Name --[[ Get the name of the part ]])
        end
	end
end

To get the position of the front ledge, do something like this:

LedgePosOfsomepart = somepart.CFrame * CFrame.new(0, somepart.Size.Y / 2, somepart.Size.Z / -2)

Play with this and adjust it to your game as needed.

2 Likes
game:GetService("Players").LocalPlayer:DistanceFromCharacter(somePart.Position + Vector3.new(0, somePart.Size.Y / 2, somePart.Size.Z / -2))
-- (I took some of the code from imalex4 reply)

I think this is it