Running system issue

Hello everyone I was working on a running system with animation for R6 characters when I was testing it it was working perfectly until the I started running and rotating then i found the animation was like stop and playing and the audio too

Here is the local Script

UIS.InputBegan:Connect(function(Key)--Running Function
	
	if Key.KeyCode == Enum.KeyCode.LeftShift and canRun == true  then
		Running = true
		canDo =false
		Humanoid.WalkSpeed = 30
		Humanoid.JumpPower = 0
		canRun = false
		Humanoid.Running:Connect(function(Speed)
			if Speed > 0 and Humanoid.WalkSpeed ~=9 and Running == true and canRun == false then --When holding shift and player is running
				Load3:Play()
				script["Running Sound"]:Play()
				tween3:Play()
			elseif Speed == 0   then
				script["Running Sound"]:Stop()
				Load3:Stop()
		  tween4:Play()
			end
			UIS.InputEnded:Connect(function(Key)

				if Key.KeyCode == Enum.KeyCode.LeftShift and canRun == false and crouched == false and sliding == false then
					script["Running Sound"]:Stop()
					tween4:Play()
					canDo = true
					canRun = true
					Running = false
					Humanoid.JumpPower = 50
					Humanoid.WalkSpeed = 16
					Load3:Stop()
				end

			end)

		end)
	end
end)

robloxapp-20240708-2004054.wmv (2.0 MB)

Thanks for any help.

1 Like

It might be because you are only checking the input for the shift key. Any other input will replace the movement animation.

Try searching the forums for ‘sprint script’ to see if your problem has been answered in another post.

2 Likes

I tried that but couldn’t find my issue

1 Like

This code has several issues with logic and performance that need to be addressed. Here are a few recommendations for improvement:

  1. Avoid Nesting InputEnded Inside InputBegan: Nesting InputEnded inside InputBegan can lead to multiple unnecessary connections being created. It’s better to handle InputEnded separately.

  2. Separate the Running:Connect Event: Connecting Running:Connect inside the InputBegan event will create multiple connections every time the LeftShift key is pressed. This is inefficient and can degrade performance over time.

  3. Improve Code Readability and Organization: Refactor the code by creating separate functions for starting and stopping the running state. This approach will be better:

local UIS = game:GetService("UserInputService")
local Running = false
local canRun = true
local canDo = false
local crouched = false
local sliding = false

local function startRunning()
    if canRun and not Running then
        Running = true
        canDo = false
        canRun = false
        Humanoid.WalkSpeed = 30
        Humanoid.JumpPower = 0
        script["Running Sound"]:Play()
        tween3:Play()
    end
end

local function stopRunning()
    if Running then
        Running = false
        canDo = true
        canRun = true
        Humanoid.WalkSpeed = 16
        Humanoid.JumpPower = 50
        script["Running Sound"]:Stop()
        tween4:Play()
    end
end

UIS.InputBegan:Connect(function(Key)
    if Key.KeyCode == Enum.KeyCode.LeftShift then
        startRunning()
    end
end)

UIS.InputEnded:Connect(function(Key)
    if Key.KeyCode == Enum.KeyCode.LeftShift and not crouched and not sliding then
        stopRunning()
    end
end)

Humanoid.Running:Connect(function(Speed)
    if Speed > 0 and Running then
        Load3:Play()
    elseif Speed == 0 then
        Load3:Stop()
    end
end)

This might not address your issue but it’s just painful reading that code.

2 Likes

it stopped the issue but the player is now mdoing the animation when not running , its like the walking animation

I think the humanoid running logic is wrong, try this:

Humanoid.Running:Connect(function(Speed)
    if Speed > 0 then
        if Running then
            Load3:Play()
        else
            Load3:Stop()
        end
    elseif Speed == 0 then
        Load3:Stop()
    end
end)

It worked but the animation is a bit laggy when moving around but the sound works

No worries i added my script back and made the “Running” bool turns true not when holding shift ,when holding shift and player is running anyways thank you all for your help

You could also have made it
if Speed < .1 then instead of if Speed == 0 then
Otherwise if the speed is .0000001 then the animation won’t stop.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.