Climbing to the left and right

I would need to edit the script to repeat the animation of the direction when the player holds the pressed direction (A - LEFT) and (D -Right) key.
I don’t know anymore, the animation is still not repeated

video example:

script:

local CollectionService = game:GetService("CollectionService")
local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local Root = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
local Human = Character:WaitForChild("Humanoid")

local Drzet = Human:LoadAnimation(script:WaitForChild("HoldAnim"))
local Vyset = Human:LoadAnimation(script:WaitForChild("Vyset"))

local NalevoVlevo = Human:LoadAnimation(script:WaitForChild("VlevoVlevo"))
local NapravoVpravo = Human:LoadAnimation(script:WaitForChild("VpravoVpravo"))

local TouchGui = plr:WaitForChild("PlayerGui"):FindFirstChild("TouchGui")
local UIS = game:GetService("UserInputService")
local cd = false
local ledgeavailable = true
local holding = false
local gyro
local LVele
local partCF
local LedgeAtt
local cross
local num
local UI = game.Players.LocalPlayer.PlayerGui.StarterGui.Frame
UI.Visible = false

local leftHolding = false
local rightHolding = false

function left()
    leftHolding = true
    if LVele then
        LVele:Destroy()
    end
    Human.AutoRotate = false
    local Vele = Instance.new("BodyVelocity", Root)
    Root.Anchored = false
    Vele.MaxForce = Vector3.new(1, 1, 1) * math.huge
    Vele.Velocity = cross * -10
    Drzet:Stop()
    Vyset:Stop()
    NalevoVlevo:Play()
    game.Debris:AddItem(Vele, .49)
    holding = false
    task.delay(.49, function()
        ledgeavailable = true
        if gyro then
            gyro:Destroy()
            NalevoVlevo:Stop()
        end
        if not holding and not rightHolding then
            Human.AutoRotate = true
            UI.Visible = false
        end
    end)
end

function right()
    rightHolding = true
    if LVele then
        LVele:Destroy()
    end
    Human.AutoRotate = false
    local Vele = Instance.new("BodyVelocity", Root)
    Root.Anchored = false
    Vele.MaxForce = Vector3.new(1, 1, 1) * math.huge
    Vele.Velocity = cross * 10
    Drzet:Stop()
    Vyset:Stop()
    NapravoVpravo:Play()
    game.Debris:AddItem(Vele, .49)
    holding = false
    task.delay(.49, function()
        ledgeavailable = true
        if gyro then
            gyro:Destroy()
            NapravoVpravo:Stop()
        end
        if not holding and not leftHolding then
            Human.AutoRotate = true
            UI.Visible = false
        end
    end)
end

function stopLeft()
    leftHolding = false
    if not rightHolding then
        Human.AutoRotate = true
    end
end

function stopRight()
    rightHolding = false
    if not leftHolding then
        Human.AutoRotate = true
    end
end

function fall()
    if LVele then
        LVele:Destroy()
    end
    if gyro then
        gyro:Destroy()
    end
    local Vele = Instance.new("BodyVelocity", Root)
    Root.Anchored = false
    Vele.MaxForce = Vector3.new(1, 1, 1) * math.huge
    Vele.Velocity = Root.CFrame.LookVector * 0.5 + Vector3.new(0, -10, 0)
    Drzet:Stop()
    Vyset:Stop()
    game.Debris:AddItem(Vele, .15)
    holding = false
    UI.Visible = false
    wait(.1)
    ledgeavailable = true
    if not (leftHolding or rightHolding) then
        Human.AutoRotate = true
    end
end

UIS.InputBegan:Connect(function(Key, Chat)
    if not holding then return end 

    if Key.KeyCode == Enum.KeyCode.A and not Chat and cd == false then
        left()
        UI.Visible = true
        cd = true
        task.wait(0.6)
        cd = false
        UI.Visible = true
    end

    if Key.KeyCode == Enum.KeyCode.D and not Chat and cd == false then
        right()
        UI.Visible = true
        cd = true
        task.wait(0.6)
        cd = false
        UI.Visible = true
    end

    if Key.KeyCode == Enum.KeyCode.S and not Chat then
        fall()
    end
end)

UIS.InputEnded:Connect(function(Key)
    if Key.KeyCode == Enum.KeyCode.Q then
        NalevoVlevo:Stop()
        cd = false
    end

    if Key.KeyCode == Enum.KeyCode.A then
        stopLeft()
    end

    if Key.KeyCode == Enum.KeyCode.D then
        stopRight()
    end
end)

while game:GetService("RunService").Heartbeat:Wait() do
    local r = Ray.new(Root.CFrame.p, Root.CFrame.LookVector * 2 + Vector3.new(0, 1.5, 0))
    local part, position, normal = workspace:FindPartOnRay(r, Character)
    if part and ledgeavailable and not holding and CollectionService:HasTag(part, "Ledge") then
        if part.Size.Y >= 1 then
            if Head.Position.Y >= (part.Position.Y + (part.Size.Y / 2)) - 1 and Head.Position.Y <= part.Position.Y + (part.Size.Y / 2) and Human.FloorMaterial == Enum.Material.Air and Root.Velocity.Y <= 0 then
                holding = true
                Drzet:Play()
                ledgeavailable = false
                partCF = part.CFrame
                Human.AutoRotate = false
                if holding and not Root:FindFirstChild("Ledge") then
                    local normalCFrame = part.CFrame:ToObjectSpace(CFrame.new(Vector3.new(0, 0, 0), -normal))
                    normalCFrame = normalCFrame - normalCFrame.Position
                    local offsetCFrame = CFrame.new((part.CFrame:inverse() * Root.CFrame).Position)
                    local newCFrame = part.CFrame * offsetCFrame * normalCFrame
                    cross = Vector3.new(0, 1, 0):Cross(normal)
                    NalevoVlevo:Stop()
                    NapravoVpravo:Stop()
                    gyro = Instance.new("BodyGyro")
                    gyro.Name = "Ledge"
                    gyro.Parent = Root
                    gyro.MaxTorque = Vector3.new(1, 1, 1) * 200000
                    gyro.CFrame = newCFrame
                    gyro.P = 20000
                    LVele = Instance.new("BodyVelocity", Root)
                    LVele.Name = "LedgeVel"
                    Root.Anchored = false
                    LVele.MaxForce = Vector3.new(1, 1, 1) * math.huge
                    LVele.Velocity = Vector3.new(0, 0, 0)
                    UI.Visible = true
                end
            end
        elseif part.Size.Y < 1 then
            if Head.Position.Y >= (part.Position.Y + (part.Size.Y / 2)) - 1 and Head.Position.Y <= part.Position.Y + (part.Size.Y / 2) and Human.FloorMaterial == Enum.Material.Air and Root.Velocity.Y <= 0 then
                holding = true
                Vyset:Play()
                ledgeavailable = false
                partCF = part.CFrame
                Human.AutoRotate = false
                if holding and not Root:FindFirstChild("Ledge") then
                    local normalCFrame = part.CFrame:ToObjectSpace(CFrame.new(Vector3.new(0, 0, 0), -normal))
                    normalCFrame = normalCFrame - normalCFrame.Position
                    local offsetCFrame = CFrame.new((part.CFrame:inverse() * Root.CFrame).Position)
                    local newCFrame = part.CFrame * offsetCFrame * normalCFrame
                    cross = Vector3.new(0, 1, 0):Cross(normal)
                    NalevoVlevo:Stop()
                    NapravoVpravo:Stop()
                    gyro = Instance.new("BodyGyro")
                    gyro.Name = "Ledge"
                    gyro.Parent = Root
                    gyro.MaxTorque = Vector3.new(1, 1, 1) * 200000
                    gyro.CFrame = newCFrame
                    gyro.P = 20000
                    LVele = Instance.new("BodyVelocity", Root)
                    LVele.Name = "LedgeVel"
                    Root.Anchored = false
                    LVele.MaxForce = Vector3.new(1, 1, 1) * math.huge
                    LVele.Velocity = Vector3.new(0, 0, 0)
                    UI.Visible = true
                end
            end
        end
        if part and CollectionService:HasTag(part, "NIC") then
            UI.Visible = false
        end
    end
end

Thanks in advance for the idea

Codycheck

This is basically how I would do it.

local UIS = game:GetService("UserInputService")
local holdingLeft = false
local holdingRight = false


local function handleLeftAnimation(start)
    if start then
        if not holdingLeft then
            holdingLeft = true
            NalevoVlevo:Play()
            NalevoVlevo.Looped = true 
        end
    else
        if holdingLeft then
            holdingLeft = false
            NalevoVlevo:Stop()
        end
    end
end

local function handleRightAnimation(start)
    if start then
        if not holdingRight then
            holdingRight = true
            NapravoVpravo:Play()
            NapravoVpravo.Looped = true -- Ensure animation loops
        end
    else
        if holdingRight then
            holdingRight = false
            NapravoVpravo:Stop()
        end
    end
end


UIS.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.A then
        handleLeftAnimation(true)
    elseif input.KeyCode == Enum.KeyCode.D then
        handleRightAnimation(true)
    end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == Enum.KeyCode.A then
        handleLeftAnimation(false)
    elseif input.KeyCode == Enum.KeyCode.D then
        handleRightAnimation(false)
    end
end)

There probably more checks you would need to do tho.

1 Like