Here's an updated version of your script with the following improvements:
-
Input Handling Refactor: I've moved the input detection for the space key from RunService:BindToRenderStep to ContextActionService. This is more efficient and helps prevent the default jump behavior in Roblox, as bound actions can consume input and override default controls. By handling the space key in a custom action, we ensure that the default jump isn't triggered accidentally during climbing or ledge grabbing.
-
Jump Cooldown Implementation: I've added a 0.2-second cooldown period after climbing stops, ledge grabbing, or jumping back. During this cooldown, no climbing, ledge grabbing, or jumping can occur, which should prevent accidental jumps when you're holding space too long after reaching the top of a wall.
-
State Management: I've ensured that the script properly tracks states (e.g., isClimbing, isLedgeGrabbing) and only allows actions when appropriate. The cooldown is enforced by checking a timestamp, and it's applied in stopClimbing, LedgeGrab, and JumpBack.
-
Efficiency Improvements: The canClimb function is now only called when the space key is pressed (in the action handler), reducing unnecessary raycasts every frame. The RenderStepped bind now only handles updating the climb velocity, making it lighter.
-
Bug Fixes: This should resolve the ledge grab flinging issue by maintaining the improvements from my previous response, and the new cooldown should address the accidental jump problem.
Here's the full updated script:
-- // Services \\ --
local UIS = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService") -- Used for input handling
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
-- // 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 JumpBackAnimation = Instance.new("Animation")
JumpBackAnimation.AnimationId = "rbxassetid:// -- put ur anim Id here"
local JumpBackAnim = Hum.Animator:LoadAnimation(JumpBackAnimation) -- Renamed to avoid conflict with JumpBack function
JumpBackAnim.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 JumpCooldownTime = 0.2 -- Cooldown time in seconds after actions
-- 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 -- Tracks when jump cooldown ends
-- Helper function to check if jump cooldown is active
local function isJumpCooldownActive()
return tick() < jumpCooldownEndTime
end
local function stopClimbing()
if isClimbing then
isClimbing = false
canClimbAgain = false
jumpCooldownEndTime = tick() + JumpCooldownTime -- Set jump cooldown
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
jumpCooldownEndTime = tick() + JumpCooldownTime -- Set jump cooldown
if Climb.IsPlaying then
Climb:Stop()
end
if Vel then
Vel:Destroy()
Vel = nil
end
climbingSurface = nil
JumpBackAnim: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
local function LedgeGrab(hitNormal)
if isClimbing and not isLedgeGrabbing then
isLedgeGrabbing = true
isClimbing = false
canClimbAgain = false
ledgeGrabStartTime = tick()
jumpCooldownEndTime = tick() + JumpCooldownTime -- Set jump cooldown
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 or isJumpCooldownActive() then return end -- Prevent start if cooldown active
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 = TweenService:Create(HRP, Info, Properties)
T:Play()
repeat task.wait() until T.Completed -- Use task.wait for better practice
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
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)
local HeadRayOrigin = head.Position + Vector3.new(0, 2.5, 0)
local NeckRayOrigin = head.Position + Vector3.new(0, -1, 0)
local RayDirection = HRP.CFrame.LookVector * MaxDistance
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character} -- Ignore the character itself
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
-- ContextActionService handler for space key
local function handleClimbAction(actionName, inputState, inputObject)
if actionName == "ClimbAction" then
if inputState == Enum.UserInputState.Begin then
if isJumpCooldownActive() then return end -- Prevent any action during cooldown
local canClimbResult, canLedgeGrab, hitPart, hitPosition, hitNormal = canClimb()
if canClimbResult and canClimbAgain and hitPart.Transparency == 0 and not isClimbing and not isLedgeGrabbing then
startClimbing(hitPart, hitPosition, hitNormal)
elseif canLedgeGrab and not canClimbResult and isClimbing and not isLedgeGrabbing then
LedgeGrab(hitNormal)
else
-- If not climbing or grabbing, perform a normal jump only if on ground and not in cooldown
if Hum:GetState() == Enum.HumanoidStateType.Landed then
Hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
elseif inputState == Enum.UserInputState.End then
if isClimbing and not isLedgeGrabbing then
JumpBack()
end
end
end
end
-- Bind the action to space key with ContextActionService
ContextActionService:BindAction("ClimbAction", handleClimbAction, false, Enum.KeyCode.Space)
-- Bind to RenderStepped only for updating velocity (no input handling)
RunService:BindToRenderStep("ClimbingUpdate", Enum.RenderPriority.Character.Value, updateIntermittentVelocity)
-- Handle character removal (e.g., death or respawn)
character.AncestryChanged:Connect(function(_, parent)
if not parent then
stopClimbing()
-- Optionally stop ledge grabbing or other states if needed
end
end)
Key Changes and Explanations:
How to Test:
- Climbing and Ledge Grab: Hold space to climb walls. When you reach a ledge, it should grab and pull you up without flinging. After grabbing or stopping climb, try holding space—jumping should be disabled for 0.2 seconds.
- Accidental Jump Fix: The cooldown and input handling should prevent jumps when you're holding space too long after climbing ends. If you still experience issues, you can tweak
JumpCooldownTime (e.g., increase to 0.5 for a longer cooldown).
- Tuning: If the ledge grab still feels off, adjust
LedgeGrabForce, LedgePullForce, or LedgeGrabDuration in the constants section.
This should resolve your issues. If you encounter any new problems or need further tweaks, let me know!