Humanoid.Running function issue

Hi everyone, I need a little help with a crouch/crawling script I have been making.
Everything works fine, except when you collide with a wall while walking, the animation of crawling, in that case is the “Anim2”, starts playing even if i’m not pressing KeyCode.

The script:

    local ContextActionService = game:GetService("ContextActionService")
    local Character = script.Parent
    local Humanoid = Character:WaitForChild("Humanoid")
    local isRunning = false

    local Anim = Humanoid:LoadAnimation(Animation)
    local Anim2 = Humanoid:LoadAnimation(Animation2)
    local Anim3 = Humanoid:LoadAnimation(Animation3)



    local function Crouch(actionName, inputState, inputObject)
    	if inputState == Enum.UserInputState.Begin then
    		if not isRunning then
    			isRunning = true
    			Anim:Play()
    			Anim.Stopped:Wait()
    			Anim3:Play()
    			Humanoid.WalkSpeed = 8

    			Humanoid.Running:Connect(function(speed)
    				if speed < 10 and speed > 0.5 then
    					Anim2:Play()
    				else
    					Anim2:Stop()
    				end
    			end)
    		else
    			isRunning = false
    			Humanoid.WalkSpeed = 16
    			Anim:Stop()
    			Anim2:Stop()
    			Anim3:Stop()
    		end
    	end
    end

    ContextActionService:BindAction("Crouching", Crouch , true, Enum.KeyCode.LeftControl, Enum.KeyCode.ButtonR2)
    ContextActionService:SetPosition("Crouching", UDim2.new(1, -90, 0, -80))

I’m pretty sure the part what is causing it is the Humanoid.Running function:

Humanoid.Running:Connect(function(speed)
    				if speed < 10 and speed > 0.5 then
    					Anim2:Play()
    				else
    					Anim2:Stop()
    				end
    			end)

Extra note: The issue what is causing this, only starts if a press the KeyCode once and continues anytime i’m not crouching while getting speed lower than 10 but higher than 0.5, but it’s intended to stop the function while i’m not crouching.

To make sure the Humanoid is running you can do the following checks:

Humanoid.MoveDirection.Magnitude > 0

Humanoid.FloorMaterial ~= Enum.Material.Air

I know that, but my problem is when I’m not crouching the Humanoid.Running function still runs.
With that in mind, when i’m colliding with any objects, the player movement speed get below 10 and the function plays the animation.

The reason the function still runs is because nothing disconnects it when you uncrouch, I presume when isRunning is true when you call the crouch event, it uncrouches you? If you, you can do this

   local ContextActionService = game:GetService("ContextActionService")
    local Character = script.Parent
    local Humanoid = Character:WaitForChild("Humanoid")
    local isRunning = false
    local connection

    local Anim = Humanoid:LoadAnimation(Animation)
    local Anim2 = Humanoid:LoadAnimation(Animation2)
    local Anim3 = Humanoid:LoadAnimation(Animation3)



    local function Crouch(actionName, inputState, inputObject)
    	if inputState == Enum.UserInputState.Begin then
    		if not isRunning then
    			isRunning = true
    			Anim:Play()
    			Anim.Stopped:Wait()
    			Anim3:Play()
    			Humanoid.WalkSpeed = 8

    			connection = Humanoid.Running:Connect(function(speed)
    				if speed < 10 and speed > 0.5 then
    					Anim2:Play()
    				else
    					Anim2:Stop()
    				end
    			end)
    		else
    			isRunning = false
    			Humanoid.WalkSpeed = 16
    			Anim:Stop()
    			Anim2:Stop()
    			Anim3:Stop()
                if connection then connection:Disconnect() end
    		end
    	end
    end

    ContextActionService:BindAction("Crouching", Crouch , true, Enum.KeyCode.LeftControl, Enum.KeyCode.ButtonR2)
    ContextActionService:SetPosition("Crouching", UDim2.new(1, -90, 0, -80))

This will make it so if the crouching is running, check if there’s a connection and if there is, disconnect it, this will make it so when you’re not crouching, the event does not run anymore, which is hopefully what you wanted

Yeah, the crouch function works fine, the only issue is the one I have related above.
I will try out this change. Thanks for the help :slight_smile:

I’m not quite understanding your other issue, what’s the problem you’re also facing?

The only issue in your code is what you forgot to put a End on the connection function.

if connection then	
	connection:Disconnect()
end

The only problem is the same, when a bump in any objects what reduces my speed below 10, the crouch animation starts because the Humanoid.Running function doesn’t stop while i’m uncrouched. But with your solution it’s working fine now! Thanks very much for the help. :smile:

Oh right, I forgot to proof read that bit haha! I’ve edited now to add that end, thanks for pointing it out

Glad that I’ve solved your issue! If you ever have any more issues, don’t be afraid to make another post!

Now I learned thanks to you how to disconnect a function. This will help me a lot in the future. :stuck_out_tongue:

Event disconnection is certainly important, you an even disconnect an event inside of the event itself using this method! I wish you good luck in the future!

1 Like