How to fix this ledge grabbing script?

So I recently found this cool ledge-grabbing script that I was messing around with. I noticed that it only works when there is one part, here is an example of where it would work

Here is where it won’t work:

As you can see, it does not work where there are two parts right on top of each other. Can anyone tell be a way to get pass this. I have a general understanding of the code but not enough to fix it myself.

Script:

--services
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local GuiService = game:GetService("GuiService")

--varibles
local player = game.Players.LocalPlayer
local character = player.Character
local camera = workspace.CurrentCamera
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")

--modules
local cameraShaker = require(game.ReplicatedStorage:WaitForChild("Modules").CameraShaker)

--shake camera
local function ShakeCamera(shakeCf)
	camera.CFrame = camera.CFrame * shakeCf
end

-- Create CameraShaker instance:
local renderPriority = Enum.RenderPriority.Camera.Value + 1
local camShake = cameraShaker.new(renderPriority, ShakeCamera)

--vaulting
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

local vaultMoveNumber = 10
local canVault = true
local canMove = true
local vaultConnection = nil
local ledgePart = nil

local grabAnim = humanoid:LoadAnimation(script:WaitForChild("Grab"))
local grabRightAnim = humanoid:LoadAnimation(script:WaitForChild("GrabRight"))
local grabLeftAnim = humanoid:LoadAnimation(script:WaitForChild("GrabLeft"))
local grabSounds = script:WaitForChild("Sounds"):GetChildren()

--play vault sounds
local function playSound()
	local sound = grabSounds[math.random(1, #grabSounds)]:Clone()
	sound.Parent = rootPart
	sound.Volume = .15
	sound:Play()
	
	sound.Ended:Connect(function()
		sound:Destroy()
	end)
end

--check if part is above when tryin to vault or move
local function partCheck(ledge)
	local vaultPartCheck = workspace:Raycast(ledge.Position + Vector3.new(0, -2, 0) + ledge.LookVector * 1, ledge.UpVector * 3, raycastParams)
	if vaultPartCheck == nil then
		return true
	else
		return false
	end
end

local function vaultMoveCheck(ray, anim)
	local localPos = ray.Instance.CFrame:PointToObjectSpace(ray.Position)
	local localLedgePos = Vector3.new(localPos.X, ray.Instance.Size.Y/2, localPos.Z)
	local ledgePos = ray.Instance.CFrame:PointToWorldSpace(localLedgePos)
	local ledgeOffset = CFrame.lookAt(ledgePos, ledgePos - ray.Normal)
	
	if partCheck(ledgeOffset) then
		local magnitude = (ledgePos - head.Position).Magnitude
		if magnitude < 3 then
			local info = TweenInfo.new(.4, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
			local goal = {CFrame = ledgeOffset + Vector3.new(0, -2, 0) + ledgeOffset.LookVector * -1}
			local tween = TweenService:Create(ledgePart, info, goal)
			tween:Play()
			canMove = false
			playSound()

			--screen shake
			camShake:Start()
			local dashShake = camShake:ShakeOnce(.2, 13, 0, .5)
			dashShake:StartFadeOut(.5)

			--play anim
			if anim == "Right" then
				grabRightAnim:Play()
			elseif anim == "Left" then
				grabLeftAnim:Play()
			end

			--vault move delay
			task.delay(.35, function()
				canMove = true
			end)
		end
	end
end




--detect ledges
local function detectLedge()
	if canVault and (humanoid:GetState() == Enum.HumanoidStateType.Freefall or humanoid:GetState() == Enum.HumanoidStateType.Jumping) then
		local vaultCheck = workspace:Raycast(rootPart.CFrame.Position, rootPart.CFrame.LookVector * 5, raycastParams)
		if vaultCheck then
			if vaultCheck.Instance then			
				local localPos = vaultCheck.Instance.CFrame:PointToObjectSpace(vaultCheck.Position)
				local localLedgePos = Vector3.new(localPos.X, vaultCheck.Instance.Size.Y/2, localPos.Z)
				local ledgePos = vaultCheck.Instance.CFrame:PointToWorldSpace(localLedgePos)
				local ledgeOffset = CFrame.lookAt(ledgePos, ledgePos - vaultCheck.Normal)

				local magnitude = (ledgePos - head.Position).Magnitude
				if magnitude < 6 then
					if partCheck(ledgeOffset) then
						canVault = false

						--screen shake
						camShake:Start()
						local dashShake = camShake:ShakeOnce(.36, 12, 0, .5)
						dashShake:StartFadeOut(.5)
						
						--player follows this part(you dont exactly need it but it makes tweening the player when they move easier unless there is a better way to do this but idk)
						ledgePart = Instance.new("Part")
						ledgePart.Parent = workspace
						ledgePart.Anchored = true
						ledgePart.Size = Vector3.one
						ledgePart.CFrame = ledgeOffset + Vector3.new(0, -2, 0) + ledgeOffset.LookVector * -1
						ledgePart.CanQuery = false
						ledgePart.CanCollide = false
						ledgePart.CanTouch = false
						ledgePart.Transparency = 1
						
						--play anim and sound
						game.ReplicatedStorage.Events.UpdateAttribute:FireServer("Vaulting",true)
						game.ReplicatedStorage.Events.UpdateAttribute:FireServer("CanDefend",false)
						game.ReplicatedStorage.Events.UpdateAttribute:FireServer("CanAttack",false)
						grabAnim:Play()
						playSound()
						
						--connection while player is on a ledge
						vaultConnection = RunService.RenderStepped:Connect(function(dt)
							rootPart.Anchored = true
							humanoid.AutoRotate = false -- so shift lock doesnt't rotate character
							rootPart.CFrame = rootPart.CFrame:Lerp(CFrame.lookAt(ledgePart.Position, (ledgePart.CFrame * CFrame.new(0, 0, -1)).Position), .25)
							humanoid:ChangeState(Enum.HumanoidStateType.Seated)
						end)
					end
				end
			end
		end
	elseif not canVault then
		canVault = true
		humanoid.AutoRotate = true
		rootPart.Anchored = false
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		grabAnim:Stop()
		game.ReplicatedStorage.Events.UpdateAttribute:FireServer("Vaulting",false)
		game.ReplicatedStorage.Events.UpdateAttribute:FireServer("CanDefend",true)
		game.ReplicatedStorage.Events.UpdateAttribute:FireServer("CanAttack",true)
		--check if it exists and then disconnect
		if vaultConnection then
			vaultConnection:Disconnect()
		end

		if ledgePart then
			ledgePart:Destroy()
		end
	end
end

--pc and console support
UserInputService.InputBegan:Connect(function(input, gp)
	if (input.KeyCode == Enum.KeyCode.ButtonA or input.KeyCode == Enum.KeyCode.Space) then
		detectLedge()
	end
end)

--mobile support
if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled and not UserInputService.MouseEnabled and not UserInputService.GamepadEnabled and not GuiService:IsTenFootInterface() then
	local jumpButton = player.PlayerGui:WaitForChild("TouchGui"):WaitForChild("TouchControlFrame"):WaitForChild("JumpButton")
	jumpButton.Activated:Connect(function()
		detectLedge()
	end)
end

Nevermind, found a way to fix it myself, mb.

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