Hi, I’m trying to make the running animation stop when the player is jumping.
This is what happens:
Here’s the code:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local animFile = script:WaitForChild("RunAnim")
local animn = humanoid:LoadAnimation(animFile)
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Configuration = require(game.ReplicatedStorage:WaitForChild("Configuration"))
function IsWalking()
if humanoid.MoveDirection.Magnitude > 0 then
return true
else
return false
end
end
UIS.InputBegan:Connect(function(key, gameprocess)
if key.KeyCode == Configuration.RunKey then
if IsWalking() then
animn:Play()
humanoid.WalkSpeed = Configuration.RunSpeed
TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.RunFov}):Play()
end
end
end)
UIS.InputEnded:Connect(function(key, gameprocess)
if key.KeyCode == Configuration.RunKey then
animn:Stop()
humanoid.WalkSpeed = Configuration.WalkSpeed
TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
end
end)
local runService = game:GetService("RunService")
local function PlayerNotMoving()
if humanoid.MoveDirection.Magnitude == 0 then
animn:Stop()
TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
end
end
runService.RenderStepped:Connect(PlayerNotMoving)
local function PlayerNotMoving()
if humanoid.MoveDirection.Magnitude == 0 then
animn:Stop()
TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()
elseif humanoid.FloorMaterial == Enum.Material.Air then
animn:Stop()
end
end
Try this? I just did a check if the floor material is air and stopped an animation. There are better ways of doing this however this is mostly just a demonstration.
You could just give the jump animation a higher animation priority than the run animation.
Copy the animate script inside your character, past it in the starter character scripts and swap out the jump animation with the new 1 with a higher animation priority.
If you want to continue with the method you were doing, you need to define another code where it would be able to start up again after you’ve stopped. You cut off the animation when you did animn:Stop(), you’re going to need to restart it.
I rewrite the entire code but there’s something that i don’t really understand.
Test code:
local function Run()
if PlayerJumping() == false and script:WaitForChild("Running").Value == true then
Anim:Play()
elseif PlayerJumping() == true and script:WaitForChild("Running").Value == true then
Anim:Stop()
end
end
RunService.RenderStepped:Connect(Run)
Result: The result is as expected.
Code in the input fuction:
UserInput.InputBegan:Connect(function(input, gameprocess)
if (not gameprocess) then
if PlayerWalking() then
if input.KeyCode == Enum.KeyCode.LeftShift then
script.Running.Value = true
Run()
humanoid.WalkSpeed = RunSpeed
end
end
end
end)
Result: Don’t know why works diferent.
Here’s the entire code:
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UserInput = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local NormalSpeed = 9
local RunSpeed = 24
local Camera = game.Workspace.CurrentCamera
local CameraVector = Camera.CFrame.LookVector
local Empujo = Instance.new("BodyVelocity")
local AnimFile = character:WaitForChild("Animate"):WaitForChild("run"):WaitForChild("RunAnim")
local Anim = humanoid:LoadAnimation(AnimFile)
local WalkFile = character:WaitForChild("Animate"):WaitForChild("walk"):WaitForChild("WalkAnim")
local WalkAnim = humanoid:LoadAnimation(WalkFile)
local function PlayerWalking()
if humanoid.MoveDirection.Magnitude > 0 then
return true
else
return false
end
end
local function PlayerJumping()
if humanoid.FloorMaterial == Enum.Material.Air then
return true
else
return false
end
end
local function Run()
if PlayerJumping() == false and script:WaitForChild("Running").Value == true then
Anim:Play()
elseif PlayerJumping() == true and script:WaitForChild("Running").Value == true then
Anim:Stop()
end
end
UserInput.InputBegan:Connect(function(input, gameprocess)
if (not gameprocess) then
if PlayerWalking() then
if input.KeyCode == Enum.KeyCode.LeftShift then
script.Running.Value = true
Run()
humanoid.WalkSpeed = RunSpeed
end
end
end
end)
UserInput.InputEnded:Connect(function(input, gameprocess)
if (not gameprocess) then
if input.KeyCode == Enum.KeyCode.LeftShift then
script.Running.Value = false
Anim:Stop()
humanoid.WalkSpeed = NormalSpeed
end
end
end)
The jump animation priority is higher than the run and walk animation.