How would I make this ledge grab always connect to the ledge?

Hey there! I’m making a script where you can grab onto a ledge, but the problem is, is that it doesn’t connect to the ledge correctly:

Any ways to fix this?

Important parts of the code:

local headTopRay = workspace:Raycast(humanoid.RootPart.Position+Vector3.new(0,2.5,0), humanoid.RootPart.CFrame.LookVector*2, rayParams)
local headMidRay = workspace:Blockcast(humanoid.RootPart.CFrame*CFrame.new(0,-.5,0), Vector3.new(.1,5,.1), humanoid.RootPart.CFrame.LookVector*1.5, rayParams)
	
if not headTopRay and headMidRay then
	local direction = headMidRay.Normal:Cross(Vector3.new(0, 1, 0))
	local rotation = CFrame.lookAt(Vector3.new(0,0,0), direction * Vector3.new(1, 0, 1))
	humanoid.RootPart.CFrame = CFrame.new(humanoid.RootPart.Position) * rotation
	-- ^ Rotates the player to make sure it's adjacent to the wall
	humanoid.RootPart.Anchored = true
	
	delay(0, function()
		while grabbing do
			humanoid.AutoRotate = false
			local direction = -headMidRay.Normal
			local rotation = CFrame.lookAt(Vector3.new(0,0,0), direction * Vector3.new(1, 0, 1))
			humanoid.RootPart.CFrame = CFrame.new(humanoid.RootPart.Position) * rotation
			
			runService.RenderStepped:Wait()
		end
	end)
-- Rest of the code
end

Any help is appreciated!

1 Like

So the issue is the player not actually connecting, and there being a gap?

1 Like

Why not use the ray’s position hits you’ve fired to connect the humanoid and the ledge together? If I’m not wrong, you’re not changing the position of the rootpart atall so.

It’s a blockcast, so it would be inaccurate, right?

1 Like

Yes, that’s the issue. I’ve tried moving the player forward, but that will usually result in the player going inside of the block.