How to fix running animation playing even while character standing [R6]

Hello everyone!

I have an issue with my “Shift to sprint” simple script.
So script work perfectly but running animation can be played even while character standing still

local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")

local sprintS = 17
local walkS = 10.5

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://15256643328"
anim.Parent = script
local RunAnim = script.Parent.Humanoid:LoadAnimation(anim)

local player = players.LocalPlayer
player.Character.Humanoid.WalkSpeed = walkS


local function beginSprint(input, gameProcessed)
	if not gameProcessed then        
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftShift then
				RunAnim:Play()
				RunAnim:AdjustSpeed(1.7)
				player.Character.Humanoid.WalkSpeed = sprintS
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftShift then
				RunAnim:Stop()
				player.Character.Humanoid.WalkSpeed = walkS
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)

I tried to fix that with making IF function checking left shift and wasd keycodes pressed in the same time and if its not true then animation wont play, but it results more bugs

Check the movedirection magnitude, this will tell you the length of the movement direction vector, if it’s 0 then it means they aren’t moving

local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")

local sprintS = 17
local walkS = 10.5

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://15256643328"
anim.Parent = script
local RunAnim = script.Parent.Humanoid:LoadAnimation(anim)

local player = players.LocalPlayer
player.Character.Humanoid.WalkSpeed = walkS


local function beginSprint(input, gameProcessed)
	if not gameProcessed then        
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftShift then
				RunAnim:Play()
				RunAnim:AdjustSpeed(1.7)
				player.Character.Humanoid.WalkSpeed = sprintS
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftShift and player.Character.Humanoid.MoveDirection.Magnitide > 0 then
				RunAnim:Stop()
				player.Character.Humanoid.WalkSpeed = walkS
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
1 Like

Now after i press shift once it wont stop any way. I tried movedirection magnitude checking before but it still didnt work properly, maybe because script check magnitude right after i unpress left shift but at this moment character dont stop fully and it still count as movement so animation keep playing

Then have a loop and check if the humanoid movedirection magnitude is equal to 0 and if the player is running, if both conditions are true then stop the running animation

Thank u for trying to help but it didnt help me either
BUT i found post that fixed my problem

Theres script edited by me from that post, i made settings panel so yall can change whatever you want without reading all that mess

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")

--// Settings

local walkS = 10.5 -- Walking speed
local runS = 17 -- Running Speed
local minS = 10 -- Minimum speed required to detect "walking" state from character, only change if you using walkspeed lower than 10 in ur game

local AnimS = 1.6 -- How fast running animation will play, default is 1
local AnimId = "rbxassetid://ANIMATION_ID" -- Running Animation Id

--

local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = AnimId
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Humanoid.WalkSpeed = walkS

Running = false

function Handler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
		Running = true
		Humanoid.WalkSpeed = runS
	elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
		Running = false
		if RAnimation.IsPlaying then
			RAnimation:Stop()
		end
		Humanoid.WalkSpeed = walkS
	end
end

Humanoid.Running:connect(function(Speed)
	if Speed >= 10 and Running and not RAnimation.IsPlaying then
		RAnimation:Play()
		RAnimation:AdjustSpeed(AnimS)
		Humanoid.WalkSpeed = runS
	elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = walkS
	elseif Speed < 10 and RAnimation.IsPlaying then
		RAnimation:Stop()
		Humanoid.WalkSpeed = walkS
	end
end)

Humanoid.Changed:connect(function()
	if Humanoid.Jump and RAnimation.IsPlaying then
		RAnimation:Stop()
	end
end)

game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift)

Original post:

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