Ledge climbing script not working as intended

I want my ledge climbing script to work on all sides like in this.

Correct example1

Correct example2

but when I try to climb the same ledges but on a different side, the character goes inside the ledge and is unable to move

but when it does work on a different side the player cant even go up.

this is the line of code that activates the ledge climbing

for i,v in pairs(Char:GetChildren()) do
	if v:IsA("Part") then
		v.Touched:Connect(function(touch)
			if touch.Name == "Ledge" then
				if CanClimbLedge == true then
					HRP.Anchored = true
					Hum.AutoRotate = false
					onLedge = true
					CanClimbLedge = false
					CurrentLedge = touch
					RotatePlayerEvent:FireServer(touch,HRP,Char,CurrentLedge)		
					HRP.CFrame = CFrame.new(HRP.CFrame.X,CurrentLedge.CFrame.Y,CurrentLedge.CFrame.Z) * CFrame.new(0,-2,1)
					Char:PivotTo(Char:GetPivot() * CFrame.Angles(0,math.rad(-CurrentLedge.Rotation.Y),0))	
					IdleClimbAnimation:Play()
				end
			end
		end)
	end
end

and this is the Raycasting to check if the player can go up


RunService.RenderStepped:Connect(function()
	local Head = Char:WaitForChild("Head")
	local rayParams = RaycastParams.new()
	rayParams.IgnoreWater = true
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = {script.Parent}

	local ClimbUpRayOrigin = Head.CFrame.UpVector * 3 + Head.Position
	local ClimbUpRayDirection = Head.CFrame.LookVector * 15

	local ClimbUpRayResults = workspace:Raycast(ClimbUpRayOrigin,ClimbUpRayDirection,rayParams)

	if ClimbUpRayResults  then
		if ClimbUpRayResults.Instance.Name == "Ledge" then
			CanClimbUp = true
		end
		if ClimbUpRayResults.Instance.Name ~= "Ledge" then
			CanClimbUp = false
		end
	end
	
	if CanClimbUp == true and onLedge == true and IsLeaping == false and IsPressingWInput == true then
		if ClimbUpRayResults.Instance:GetAttribute("Vertical") then
			local goal = {
				CFrame = CFrame.new(ClimbUpRayResults.Instance.Position.X,ClimbUpRayResults.Instance.Position.Y,HRP.CFrame.Z) * CFrame.new(0,-3,0)
				}
			local ti = TweenInfo.new(0.2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
			local Tween = tweenservice:Create(HRP,ti,goal)
			Tween:Play()
		end
	end
end)

what is wrong?

I have been trying to fix this bug for 2 days, if someone knows why it does this please reply

try replacing

HRP.CFrame = CFrame.new(HRP.CFrame.X,CurrentLedge.CFrame.Y,CurrentLedge.CFrame.Z) * CFrame.new(0,-2,1)

with

HRP.CFrame = CFrame.new(HRP.CFrame.X,CurrentLedge.CFrame.Y,CurrentLedge.CFrame.Z):ToWorldSpace(CFrame.new(0, -2, 1))

im guessing that youre offsetting the player position by 0, -2, 1 but not applying rotation to the offset with * CFrame.new(0, -2, 1) which makes the character go into the wall, replacing it with :ToWorldSpace(CFrame.new(0, -2, 1)) does apply rotation

Sadly this didn’t work, but I did manage to make a temporary solution by using the ledge parts Attributes and rotating the player with CFrame.Angles

I think you also have to rotate the ledges for it to work