I need help with a ledge climbing script

Hello there I’m currently making a platformer with about amateur level of scripting, I’m currently making a using a ledge climbing mechanic for said platformer.

So let’s get to the problem like I said I don’t have a lot of experience in scripting so I clearly snatched a ledge climbing script from the toolbox and at first sight, it looks just fine and it’s the best one I have seen but the problem is the raycasting is a bit off and sometimes the player wouldn’t stick on a ledge but rather the… air, yes.
here’s footage and script bdw :

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
local Hum = Character:WaitForChild("Humanoid")
local CA = Hum:LoadAnimation(script:WaitForChild("ClimbAnim"))
local HA = Hum:LoadAnimation(script:WaitForChild("HoldAnim"))
local HSA = Hum:LoadAnimation(script:WaitForChild("HoldSAnim"))
local TouchGui = plr:WaitForChild("PlayerGui"):FindFirstChild("TouchGui")
local UIS = game:GetService("UserInputService")

ledgeavailable = true
holding = false

while game:GetService("RunService").Heartbeat:Wait() do
	local r = Ray.new(Head.CFrame.p, Head.CFrame.LookVector * 100)
	local part,position = workspace:FindPartOnRay(r,Character)
	if part and ledgeavailable  and not holding then
		if part.Size.Y >= 7 then
			if Head.Position.Y >= (part.Position.Y + (part.Size.Y / 2)) - 1 and Head.Position.Y <= part.Position.Y + (part.Size.Y / 2) and Hum.FloorMaterial == Enum.Material.Air and Root.Velocity.Y <= 0 then
				wait(0.05)
				Root.Anchored = true 
				holding = true 
				ledgeavailable = false
				HA:Play()
				wait(0)
				HSA:Play()
			end
		end
	end
	
	function climb()
		local Vele = Instance.new("BodyVelocity",Root)
		Root.Anchored = false
		Vele.MaxForce = Vector3.new(1,1,1) * math.huge
		Vele.Velocity = Root.CFrame.LookVector * 10 + Vector3.new(0,30,0)
		HA:Stop() CA:Play()
		game.Debris:AddItem(Vele,.15)
		holding = false
		wait(.75)
		ledgeavailable = true
	end
	
	UIS.InputBegan:Connect(function(Key,Chat)
		if not holding then return end 
		if Key.KeyCode == Enum.KeyCode.Space and not Chat then
			climb()
		end
	end)
	
	if TouchGui then
		TouchGui:WaitForChild("TouchControlFrame"):WaitForChild("JumpButton").MouseButton1Click:Connect(function()
			if not holding then return end climb()
		end)
	end
end

Yes, I have been looking for solutions and making my own by using .Touched event on the root part of the character so I could at least make the character… not stick on some virtual O2 but it just stays the same and nothing changes, It would be nice if someone could find me a solution to this. like using TouchInterest or?

1 Like

The issue is this line: local r = Ray.new(Head.CFrame.p, Head.CFrame.LookVector * 100)
The ray has a length of 100 studs here, meaning it might catch on anything in a range of 100 studs.
I’d recommend lowering the 100 to a number closer to perhaps 3.

2 Likes

that-
that actually worked-
man since i know nothing about raycasting I thought the creator of this toolbox model set it to the right number, wow I cant believe it
thank you kind stranger

I highly recommend reading up on raycasts, especially if you’re going to create a movement system. They’re very useful.