Wallclimbing bug, need help

I have a wallcimbing script, it works fine but when it tries to climb an edge it just freaks out.

Could someone might help me make it to where if it finds an edge it can’t climb it?

I don’t know how to approach this.

Video:

Script:

game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	if stateManager.GetStates('Rolling') == true or stateManager.GetStates('Vaulting') == true or stateManager.GetStates('Sliding') == true or stateManager.GetStates('WallRunningLeft') == true or stateManager.GetStates('WallRunningRight') == true or stateManager.GetStates('Reach') == true or stateManager.GetStates('Springboard') == true or stateManager.GetStates('PreciseLand') == true then
		return
	end

	local rootPart = getHumanoidRootPart(c)

	if not rootPart then
		return
	end

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {c}
	raycastParams.IgnoreWater = true

	local position = safeGetHumanoidRootPartPosition(c)
	local cframe = safeGetHumanoidRootPartCFrame(c)

	local rayUp = workspace:Raycast(position, cframe.LookVector * 1.5, raycastParams)
	local rayDown = workspace:Raycast(position, -cframe.UpVector * fallThreshold, raycastParams)
	local ray = rayUp

	if not ray and not canWallclimb and not hasWallClimbed then
		canWallclimb = true
	end

	if ray and holdingKey and not canWallclimb and not collectionService:HasTag(ray.Instance, "PIPE") then
		if ray.Instance and ray.Instance:IsA("BasePart") then
			hasWallClimbed = true
			wallClimbing = true
			
			Animations["CoilAnim"]:Stop()
			Animations["EdgeJumpAnim"]:Stop()
			Animations["HighJumpAnim"]:Stop()
			Animations["LongJumpAnim"]:Stop()

			stateManager.SetState("WallClimbing", true)

			local wallNormal = ray.Normal
			local lookDirection = -wallNormal
			local upVector = Vector3.new(0, 2, 0)

			c.HumanoidRootPart.CFrame = CFrame.lookAt(c.HumanoidRootPart.Position, c.HumanoidRootPart.Position + lookDirection, upVector)

			wallClimbTimer = wallClimbTimer + deltaTime

			local cooldownTime = maxWallClimbTime - wallClimbTimer

			local part = ray.Instance

			local weld = c.HumanoidRootPart:FindFirstChild("WallclimbWeld") or Instance.new("WeldConstraint")
			weld.Name = "WallclimbWeld"
			weld.Part0 = c.HumanoidRootPart
			weld.Part1 = part

			local bp = c.HumanoidRootPart:FindFirstChild("WallClimbBodyPosition") or Instance.new("BodyPosition", c.HumanoidRootPart)
			bp.Name = "WallClimbBodyPosition"
			bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			local dampingFactor = 1.1 - math.min(wallClimbTimer / maxWallClimbTime, 1.1)

			local minDampingFactor = 0
			dampingFactor = math.max(dampingFactor, minDampingFactor)

			local maxSpeed = 35
			local walkSpeed = math.min(c.Humanoid.WalkSpeed, maxSpeed)
			local adjustedForce = walkSpeed * 0.5 * dampingFactor
			bp.Position = c.HumanoidRootPart.Position + Vector3.new(0, adjustedForce, 0)

			if Animations["WallClimbAnim"].IsPlaying then
				Animations["WallClimbAnim"]:AdjustSpeed(dampingFactor)
			end

			local partTop = part.Position.Y + (part.Size.Y / 2)
			if rootPart.Position.Y >= partTop - 3 and rootPart.Position.Y <= partTop then
				if not hasVault then
					Animations["WallClimbAnim"]:Stop()
					performAction("Vault", part)
					hasVault = true
					wallClimbing = false
				end
			end
			
			if wallClimbTimer >= maxWallClimbTime then
				if holdingKey then
					local downwardForce = 3
					bp.Position = c.HumanoidRootPart.Position + Vector3.new(0, -downwardForce, 0)
				end
			end
		end
	else
		for i, child in pairs(c.HumanoidRootPart:GetChildren()) do
			if child.Name == "WallclimbWeld" or child.Name == "WallClimbBodyPosition" then
				c.Humanoid.WalkSpeed = originalWalkSpeed
				child:Destroy()
			end
		end
		
		wallClimbing = false
		stateManager.SetState("WallClimbing", false)
		Animations["WallClimbAnim"]:Stop()
		wallClimbTimer = 0

		if sound["Wallclimb"].IsPlaying then
			sound["Wallclimb"]:Stop()
		end
	end
end)

I haven’t looked over the code, but try using a system to where you have to be at above certain threshold to switch sides. So when you’re on the edge, you have to move over more to switch sides and you don’t just glitch.