Climbing script isnt working

i want the player to be able to climb a part and if the player is above the part, the climbing stops along with the animation.

once the player is above any part it climbs, it breaks the script causing the player to be unable to fully climb any other part.

ive tried fixing it myself and looking for solutions, however none of the them have worked.

the code

local uis = game:GetService("UserInputService")

local cd = false

local hrp = script.Parent.Head

local hum = script.Parent:FindFirstChildWhichIsA("Humanoid")

local ClimbStart = hum:LoadAnimation(script.Start)



local ExtraClimb = false

uis.InputBegan:Connect(function(key,istyp)
	if istyp then return end
	if key.KeyCode == Enum.KeyCode.Space then
		local raycast = workspace:Raycast(hrp.Position,hrp.CFrame.LookVector*15)

		if raycast then
			if raycast.Instance then
				if hrp.Position.Y < raycast.Instance.Position.Y+(raycast.Instance.Size.Y/2) and cd == false and hum.FloorMaterial == Enum.Material.Air and raycast.Distance <= 8 then
					script.Parent.HumanoidRootPart.Velocity = Vector3.new(0,5,0)
					ClimbStart:Play()
					cd = true
					game.ReplicatedStorage.ChangeValue:FireServer(script.Parent.WalkSpeed,0)
					--print("Climb one")
					local i = Instance.new("BodyVelocity")
					i.Parent = hrp
					ClimbStart.KeyframeReached:Connect(function(kf)
						if kf == "Climb1" then
							game.ReplicatedStorage.GiveStun:FireServer(0.02)
							i.MaxForce = Vector3.new(0,1,0)*6000
							i.P = 15000
							i.Velocity = hrp.CFrame.UpVector * 25
						elseif kf == "Climb2" then
							if hrp.Position.Y < raycast.Instance.Position.Y+(raycast.Instance.Size.Y/2) and hum.FloorMaterial == Enum.Material.Air and raycast.Distance <= 8 then
								if ExtraClimb == false then

									ExtraClimb = true
									--print("Climb two")
									i:Destroy()
									local b = Instance.new("BodyVelocity")
									b.Parent = hrp
										if isRunning == true then
											runMode = 0
											isRunning = false
											animationTrack = nil
											for i,v in pairs(char.Humanoid:GetPlayingAnimationTracks()) do
												if v.Name == "runAnimation" then
													v:Stop()
												end
											end
										end
									b.P = 15000
									b.MaxForce = Vector3.new(0,1,0)*6000
									b.Velocity = hrp.CFrame.UpVector * 45
									game:GetService("Debris"):AddItem(b,0.2)
								end
							else

								i:Destroy()
								ClimbStart:Stop()
								--raycast.Instance.Color = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
								game.ReplicatedStorage.ChangeValue:FireServer(script.Parent.WalkSpeed,14)
							end
						end
					end)

					repeat
						task.wait(0.1)
					until hum.FloorMaterial ~= Enum.Material.Air
					if script.Parent.WalkSpeed.Value == 0 then
						game.ReplicatedStorage.ChangeValue:FireServer(script.Parent.WalkSpeed,14)
					end
					task.wait(0.5)
					ExtraClimb = false
					cd = false
				end
			end
		end
	end
end)

help would be appreciated.