Problem with a crouch script

How would I disable crouch in this script when a tool is equipped using
character.ChildAdded:Connect(function(tool)
if tool:IsA(“Tool”) then

local Player = game.Players.LocalPlayer
local Character = script.Parent

local UIS = game:GetService("UserInputService")

local Crouching = false

local CrouchIdleID = 8756542650
local CrouchWalkID = 8756518128
script:WaitForChild("CrouchIdle").AnimationId = "rbxassetid://"..CrouchIdleID
script:WaitForChild("CrouchWalk").AnimationId = "rbxassetid://"..CrouchWalkID

local CrouchIdle = Character.Humanoid:LoadAnimation(script:WaitForChild("CrouchIdle"))
local CrouchWalk = Character.Humanoid:LoadAnimation(script:WaitForChild("CrouchWalk"))

UIS.InputBegan:Connect(function(input, typing)
    if input.KeyCode == Enum.KeyCode.C and not typing then

        Crouching = true
        Character.Humanoid:UnequipTools()

        Character.Humanoid.WalkSpeed = 7
        Character.Humanoid.JumpPower = 0

        CrouchIdle:Play(0.3)

        Character.Humanoid.Running:Connect(function(speed)
            if Crouching then
                if speed == 0 then
                    CrouchIdle:Play(0.3)
                    CrouchWalk:Stop(0.3)
                elseif speed > 0 then
                    CrouchIdle:Stop(0.3)
                    CrouchWalk:Play(0.3)
                end
            end
        end)
    end
end)

UIS.InputEnded:Connect(function(input, typing)
    if input.KeyCode == Enum.KeyCode.C and not typing then

        Crouching = false

        Character.Humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
        Character.Humanoid.JumpPower = game.StarterPlayer.CharacterJumpPower

        CrouchIdle:Stop(0.3)
        CrouchWalk:Stop(0.3)


    end
end)```

Allow me a minute to draft you something with explanation.

1 Like

I simplified the crouch and stop-crouch codes into functions for you.

local Player = game.Players.LocalPlayer
local Character = script.Parent

local UIS = game:GetService("UserInputService")

local Crouching = false

local CrouchIdleID = 8756542650
local CrouchWalkID = 8756518128
script:WaitForChild("CrouchIdle").AnimationId = "rbxassetid://"..CrouchIdleID
script:WaitForChild("CrouchWalk").AnimationId = "rbxassetid://"..CrouchWalkID

local CrouchIdle = Character.Humanoid:LoadAnimation(script:WaitForChild("CrouchIdle"))
local CrouchWalk = Character.Humanoid:LoadAnimation(script:WaitForChild("CrouchWalk"))

local function crouch()
    Crouching = true
    Character.Humanoid:UnequipTools()

    Character.Humanoid.WalkSpeed = 7
    Character.Humanoid.JumpPower = 0

    CrouchIdle:Play(0.3)

    Character.Humanoid.Running:Connect(function(speed)
        if Crouching then
            if speed == 0 then
                CrouchIdle:Play(0.3)
                CrouchWalk:Stop(0.3)
            elseif speed > 0 then
                CrouchIdle:Stop(0.3)
                CrouchWalk:Play(0.3)
            end
        end
    end)
end
    
local function stopCrouching()
    Crouching = false

    Character.Humanoid.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed
    Character.Humanoid.JumpPower = game.StarterPlayer.CharacterJumpPower

    CrouchIdle:Stop(0.3)
    CrouchWalk:Stop(0.3)
end

UIS.InputBegan:Connect(function(input, typing)
    if input.KeyCode == Enum.KeyCode.C and not typing then
        crouch()
    end
end)

UIS.InputEnded:Connect(function(input, typing)
    if input.KeyCode == Enum.KeyCode.C and not typing then
        stopCrouching()
    end
end)

Character.ChildAdded:Connect(function(tool)
    if tool:IsA("Tool") then
       stopCrouching() 
    end
end)

There are two event listeners that both, if the circumstances meet the criteria, run the stopCrouching function. Which, as the name says, stops the action of crouching.

Assuming the crouch functions themselves work, this code should work fine. Otherwise, let me know and I might need to draft another way of detecting the action of equipping a tool.

Disregard my deleted reply. @EugeneFaung let me know how that code works.

1 Like

Hi! I apologize for the late response. The code works great! Thank you very much for your help.

Glad I was able to help. Have a good night :heart: and good luck with your project.

1 Like
local function crouch()
    Crouching = true
    Character.Humanoid:UnequipTools()

    Character.Humanoid.WalkSpeed = 7
    Character.Humanoid.JumpPower = 0

    CrouchIdle:Play(0.3)

    Character.Humanoid.Running:Connect(function(speed)
        if Crouching then
            if speed == 0 then
                CrouchIdle:Play(0.3)
                CrouchWalk:Stop(0.3)
            elseif speed > 0 then
                CrouchIdle:Stop(0.3)
                CrouchWalk:Play(0.3)
            end
        end
    end)
end

Could cause a memory leak.

Yes, thank you for pointing that out. Simple solution is to move the Running listener outside of the function. I just kept the script as it was originally.