I'll modify the script to disable jumping for a short period after a ledge grab to prevent accidental jumps when reaching the top. Here's the updated version:
-- // Services \\ --
local UIS = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- // Local Player & Character Variables \\ --
local localPlr = Players.LocalPlayer
local character = localPlr.Character or localPlr.CharacterAdded:Wait()
local HRP = character:WaitForChild("HumanoidRootPart")
local Hum = character:WaitForChild("Humanoid")
local head = character:WaitForChild("Head")
local ClimbAnimation = Instance.new("Animation")
ClimbAnimation.AnimationId = "rbxassetid:// -- put ur anim Id here"
local Climb = Hum.Animator:LoadAnimation(ClimbAnimation)
Climb.Priority = Enum.AnimationPriority.Action2
local GrabAnimation = Instance.new("Animation")
GrabAnimation.AnimationId = "rbxassetid:// -- put ur anim Id here"
local Grab = Hum.Animator:LoadAnimation(GrabAnimation)
Grab.Priority = Enum.AnimationPriority.Action2
local JumpBack = Instance.new("Animation")
JumpBack.AnimationId = "rbxassetid:// -- put ur anim Id here"
local Jump = Hum.Animator:LoadAnimation(JumpBack)
Jump.Priority = Enum.AnimationPriority.Action2
-- Climbing Constants
local MaxUpwardsVel = 30
local ClimbDuration = 1 -- Time to decrease upward velocity to 0
local MaxDistance = 10
local LedgeGrabForce = 35 -- Vertical force for ledge grab
local LedgePullForce = 20 -- Horizontal force to pull toward ledge
local LedgeGrabDuration = 0.3 -- Time to apply ledge grab forces
local JumpCooldown = 0.2 -- Time to disable jumping after ledge grab
-- Climbing State variables
local isClimbing = false
local isLedgeGrabbing = false
local climbStartTime = 0
local Vel = nil
local UpwardsVel = MaxUpwardsVel
local climbingSurface = nil
local canClimbAgain = true
local ledgeGrabStartTime = 0
local jumpCooldownEndTime = 0
local function stopClimbing()
if isClimbing then
isClimbing = false
canClimbAgain = false
if Climb.IsPlaying then
Climb:Stop()
end
if Vel then
Vel:Destroy()
Vel = nil
end
climbingSurface = nil
end
end
local function JumpBack()
if isClimbing then
isClimbing = false
canClimbAgain = false
if Climb.IsPlaying then
Climb:Stop()
end
if Vel then
Vel:Destroy()
Vel = nil
end
climbingSurface = nil
Jump:Play()
local newVel = Instance.new("BodyVelocity")
newVel.Name = "JumpBackVelocity"
newVel.Parent = HRP
newVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
newVel.Velocity = Vector3.new(0, 20, 0) + HRP.CFrame.LookVector * -30
task.wait(0.1)
newVel:Destroy()
end
end
-- Improved Ledge grabbing logic with jump cooldown
local function LedgeGrab(hitNormal)
if isClimbing and not isLedgeGrabbing then
isLedgeGrabbing = true
isClimbing = false
canClimbAgain = false
ledgeGrabStartTime = tick()
jumpCooldownEndTime = tick() + JumpCooldown
if Climb.IsPlaying then
Climb:Stop()
end
if Vel then
Vel:Destroy()
Vel = nil
end
climbingSurface = nil
Grab:Play()
-- Apply initial upward force
local upwardForce = Instance.new("BodyVelocity")
upwardForce.Name = "LedgeGrabUpward"
upwardForce.Parent = HRP
upwardForce.MaxForce = Vector3.new(0, math.huge, 0)
upwardForce.Velocity = Vector3.new(0, LedgeGrabForce, 0)
task.wait(0.1)
upwardForce:Destroy()
-- Apply horizontal pull toward ledge
local pullForce = Instance.new("BodyVelocity")
pullForce.Name = "LedgeGrabPull"
pullForce.Parent = HRP
pullForce.MaxForce = Vector3.new(math.huge, 0, math.huge)
pullForce.Velocity = HRP.CFrame.LookVector * LedgePullForce
task.wait(LedgeGrabDuration)
pullForce:Destroy()
-- Stop the grab animation after a short delay
task.wait(0.5)
if Grab.IsPlaying then
Grab:Stop()
end
isLedgeGrabbing = false
end
end
-- Helper function to start climbing
local function startClimbing(hitPart, hitPosition, hitNormal)
if isClimbing or not canClimbAgain then return end -- Prevents repeated activation
isClimbing = true
climbStartTime = tick()
UpwardsVel = MaxUpwardsVel
climbingSurface = hitPart
-- Snap character to the climbing surface
local offset = hitNormal * (character.HumanoidRootPart.Size.Z / 2 + 0.5)
local newPosition = hitPosition + offset
local Properties = {CFrame = CFrame.new(newPosition, newPosition - hitNormal) }
local Info = TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local T = game:GetService("TweenService"):Create(HRP, Info, Properties)
T:Play()
repeat wait() until T.Completed
HRP.CFrame = CFrame.new(newPosition, newPosition - hitNormal)
Vel = Instance.new("BodyVelocity")
Vel.Parent = HRP
Vel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
Vel.Velocity = Vector3.new(0, UpwardsVel, 0)
Climb:Play()
end
-- Update climb velocity over time (smooth deceleration)
local function updateIntermittentVelocity()
if isClimbing and Vel then
local elapsedTime = tick() - climbStartTime
local progress = math.min(elapsedTime / ClimbDuration, 1)
UpwardsVel = MaxUpwardsVel * (1 - progress)
Vel.Velocity = Vector3.new(0, UpwardsVel, 0)
Climb:AdjustSpeed(UpwardsVel / MaxUpwardsVel)
if progress >= 1 then
stopClimbing()
end
end
end
-- Function to check if jump is allowed
local function canJump()
return tick() >= jumpCooldownEndTime
end
Hum.StateChanged:Connect(function(_, newState)
if newState == Enum.HumanoidStateType.Landed then
canClimbAgain = true
end
end)
-- Check if climbing conditions are met (raycasting)
local function canClimb()
local BodyRayOrigin = HRP.Position + Vector3.new(0, 0, 0) -- Slightly below HumanoidRootPart for body detection.
local HeadRayOrigin = head.Position + Vector3.new(0, 2.5, 0) -- Slightly above the head for ledge detection.
local NeckRayOrigin = head.Position + Vector3.new(0,-1,0)
local RayDirection = HRP.CFrame.LookVector * (MaxDistance)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.IgnoreWater = true
local BodyRaycastResult = workspace:Raycast(BodyRayOrigin, RayDirection, raycastParams)
local HeadRaycastResult = workspace:Raycast(HeadRayOrigin, RayDirection, raycastParams)
local NeckRaycastResult = workspace:Raycast(NeckRayOrigin, RayDirection, raycastParams)
if BodyRaycastResult and HeadRaycastResult and NeckRaycastResult then
return true, false, BodyRaycastResult.Instance, BodyRaycastResult.Position, BodyRaycastResult.Normal
elseif BodyRaycastResult and not HeadRaycastResult then
return false, true, BodyRaycastResult.Instance, BodyRaycastResult.Position, BodyRaycastResult.Normal
end
return false, false
end
RunService:BindToRenderStep("Climbing", Enum.RenderPriority.Input.Value, function()
local canClimbResult, canLedgeGrab, hitPart, hitPosition, hitNormal = canClimb()
-- Check for jump cooldown
if not canJump() and UIS:IsKeyDown(Enum.KeyCode.Space) then
-- Prevent jumping during cooldown
return
end
if UIS:IsKeyDown(Enum.KeyCode.Space) and canClimbResult and canClimbAgain and hitPart.Transparency == 0 and not isClimbing and not isLedgeGrabbing then
startClimbing(hitPart, hitPosition, hitNormal)
elseif UIS:IsKeyDown(Enum.KeyCode.Space) and canLedgeGrab and not canClimbResult and isClimbing and not isLedgeGrabbing then
LedgeGrab(hitNormal)
elseif not UIS:IsKeyDown(Enum.KeyCode.Space) and isClimbing and not isLedgeGrabbing then
JumpBack()
end
updateIntermittentVelocity()
end)
character.AncestryChanged:Connect(function(_, parent)
if not parent then
stopClimbing()
end
end)
Key changes made:
- Added a
JumpCooldown constant (0.2 seconds) that determines how long jumping should be disabled after a ledge grab
- Added a
jumpCooldownEndTime variable to track when the cooldown ends
- Added a
canJump() function that checks if the cooldown period has passed
- Modified the ledge grab function to set the jump cooldown when a ledge grab occurs
- Added a check at the beginning of the render step to prevent jumping during the cooldown period
This will prevent the character from jumping immediately after a ledge grab, giving you time to release the space key before the jump happens. You can adjust the JumpCooldown value to make the cooldown longer or shorter as needed.
The cooldown only applies to the automatic jump that happens when you release the space key, so you can still manually jump after the cooldown period if you want to.