Help to stop playing animtions while running

i need help to stop the animation playing while sprinting:

block of code:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = nil

local camera = game.Workspace.CurrentCamera

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Configuration = require(game.ReplicatedStorage.Modules:WaitForChild("Configuration"))

function IsWalking()
	if humanoid.MoveDirection.Magnitude > 0 then
		return true
	else
		return false
	end
end

UIS.InputBegan:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		if IsWalking() then
			animation = humanoid:LoadAnimation(script.Animation)
			animation:Play(0.1, 1, 2)
			humanoid.JumpPower = 0
			humanoid.WalkSpeed = humanoid.WalkSpeed + 9
			TweenService:Create(camera, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView + 10}):Play()
		end
	end
end)

UIS.InputEnded:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		animation:Stop()
		humanoid.JumpPower = 50
		humanoid.WalkSpeed = humanoid.WalkSpeed - 9
		TweenService:Create(camera, TweenInfo.new(0.2 , Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView - 10}):Play()
	end
end)

video:

External Media

I don’t know why the code seems very fine but it won’t work, maybe try to put the animations into a RunService

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local animation = nil

local camera = game.Workspace.CurrentCamera



local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
--Add a RunService
local RS = game:GetService("RunService")

--Add Booleans--
local IsRunning = false
--
local Configuration = require(game.ReplicatedStorage.Modules:WaitForChild("Configuration"))

function IsWalking()
	if humanoid.MoveDirection.Magnitude > 0 then
		return true
	else
		return false
	end
end

UIS.InputBegan:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		if IsWalking() then
			animation = humanoid:LoadAnimation(script.Animation)
			IsRunning = true -- set this to true when key is pressed
			humanoid.JumpPower = 0
			humanoid.WalkSpeed = humanoid.WalkSpeed + 9
			TweenService:Create(camera, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView + 10}):Play()
		end
	end
end)


UIS.InputEnded:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		IsRunning = false -- set this to false when key is not pressed
		humanoid.JumpPower = 50
		humanoid.WalkSpeed = humanoid.WalkSpeed - 9
		TweenService:Create(camera, TweenInfo.new(0.2 , Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView - 10}):Play()
	end
end)


RS.Heartbeat:Connect(function()
	if IsRunning and IsWalking() then
		if animation.IsPlaying == false then -- check if animation not playing to prevent replaying animation 
			animation.Play(0.1,1,2)
		end
	else -- you can do elseif not IsWalking() then, either way or you can add more conditions later on
		if animation.IsPlaying == true then -- just a little check if animation is playing then we stop, if already not playing then leave it
			animation:Stop()
		end
	end
end)

let me know if it works

1 Like

So is it when you stop moving and still have the key pressed down you wont stop the running animation? If so that’s because you arent checking whether the player has stopped moving or not. Only time you check it is when you press the key, which naturally means if the animation is looped there will be nothing to stop it until you depress the key.

Use runservice for this. But before that put a local variable up top as a boolean called “RunKeyPressed = false”. Set it to true when you press the runkey and false when you let go. While runservice is running if the runkey is pressed run the IsWalking() function and if it returns false then animation:stop(). Although you will get an error when you depress the key when trying to stop the animation since nothing would be playing. So you would simply add another check for that. Your code will look like this.

---Add This:
local RunService = game:GetService("RunService")
---

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = nil

local camera = game.Workspace.CurrentCamera

local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Configuration = require(game.ReplicatedStorage.Modules:WaitForChild("Configuration"))


---Add This:
local RunKeyPressed = false
local AnimStopped = false
--

function IsWalking()
	if humanoid.MoveDirection.Magnitude > 0 then
		return true
	else
		return false
	end
end

UIS.InputBegan:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
		if IsWalking() then
            ---Add This:
            RunKeyPressed = true
            AnimStopped = false
            ---
			animation = humanoid:LoadAnimation(script.Animation)
			animation:Play(0.1, 1, 2)
			humanoid.JumpPower = 0
			humanoid.WalkSpeed = humanoid.WalkSpeed + 9
			TweenService:Create(camera, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView + 10}):Play()
		end
	end
end)

UIS.InputEnded:Connect(function(key, proccess)
	if key.KeyCode == Configuration.RunKey then
      --------------------------------------Add This:
      RunKeyPressed = false
      if AnimStopped != true then
          animation:Stop()
          AnimStopped = false
      end
      humanoid.JumpPower = 50
      humanoid.WalkSpeed = humanoid.WalkSpeed - 9
      TweenService:Create(camera, TweenInfo.new(0.2 , Enum.EasingStyle.Linear, 
      Enum.EasingDirection.InOut), {FieldOfView = camera.FieldOfView - 10}):Play()
      --------------------------------------
	end
end)

---------------------------------------Add This:
local stepped
stepped = RunService.RenderStepped:Connect(function()
  if RunKeyPressed == true then
    if IsWalking() == false and AnimStopped == false then
      animation:Stop()
      AnimStopped = true
    end
  end
end)
---------------------------------------

1 Like

hi guys, i keep having this script error when i implement booleans value to my script: 21:20:42.411 AnimStopped is not a valid member of LocalScript “Workspace.TheBossOfBancon.scripts.needed.Framework.ShiftToRun” - Client - ShiftToRun:16
screenshot:
image

I think that if you define that boolean through the character it might work???:)?? and also put WaitForChild so the client has time to load their stuff

local character = game.Players.LocalPlayer.Character
local scriptsfolder = character:WaitForChild("scripts")
local neededfolder = scripts:WaitForChild("needed")
local AnimStopped = neededfolder:WaitForChild("FrameWork").ShiftToRun.AnimStopped

try it if it works

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