#I can't figure out how to play an animation when Sprinting

1 messages · Page 1 of 1 (latest)

silk briar
#

I was doing a sprinting system that uses a new animation, but idk how to make it to just plays when hes running and not when the player jumps. help

#
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local MaxStamina = script.Parent:GetAttribute("MaxStamina")
local CurrentStamina = script.Parent:GetAttribute("CurrentStamina")

local RunSpeed = script.Parent:GetAttribute("RunSpeed")
local WalkSpeed = script.Parent:GetAttribute("WalkSpeed")

local Exhausted = false
local Running = false

local CanRunValue = 25
local RecoverRate = 2.5
local DrainRate = 6

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

local AnimationsFolder = script.Parent.Animations
local Animator = Humanoid.Animator

local RunAnimation = Humanoid.Animator:LoadAnimation(AnimationsFolder.Run)

RunAnimation.Looped = true

local UI = script.Parent
local SprintBackground = UI.HolderFrame.SprintBackground
local Bar = SprintBackground.Bar

local function InputBegan(Input, GameProcess)
    if Exhausted then return end
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = RunSpeed
        Running = true
        if Humanoid.MoveDirection.Magnitude > 0 then
            RunAnimation:Play()
        end
    end
end

local function InputEnded(Input, GameProcess)
    if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
        Humanoid.WalkSpeed = WalkSpeed
        Running = false
    end
end

local function UpdateStaminaUI()
    Bar.Size = UDim2.new(math.clamp(CurrentStamina/MaxStamina, 0, 0.991), 0, 0.614,0)
end
#
    if Running and Humanoid.MoveDirection.Magnitude > 0 then
        CurrentStamina = math.max(0, CurrentStamina - DrainRate * DeltaTime)
        UpdateStaminaUI()
        
        if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
            Exhausted = true
        end
        if CurrentStamina <= 0 then
            Humanoid.WalkSpeed = WalkSpeed
            RunAnimation:Stop()
            Running = false
        end
    else 
        CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
        UpdateStaminaUI()
        if CurrentStamina >= CanRunValue then
            Exhausted = false
            RunAnimation:Stop()
        end
    end
end)

UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputEnded:Connect(InputEnded)```
next grove
#

You can use Humanoid.StateChanged

silk briar
#

thats a good one let me try

#

nah idk if i did it right

#

probably not

next grove
#

you can also detect when the player jumps then stop the animations

silk briar
#

i tried doing it

#

wait

#

well it kinda worked

next grove
#

yea it looks decnt

silk briar
#

yeah but when i jump after the animation it just stops and doesnt play anymore

next grove
#

theres enum.humanoidstatetype.landed

silk briar
#

ok

#

i can run the animation whenever iw ant

next grove
#

Well when u jump set it to freefall?

silk briar
#

yes

#

did it like this

next grove
#

Cool

silk briar
#

wdym cool?

#

its not working properly

next grove
#

Oh ok didnt read it

#

Idk u can disable jumping

#

Haha

silk briar
#

what

#

bro

next grove
#

Do u want me to fix it?

next grove
#

I thought u fixed it thats why i said cool

silk briar
silk briar
next grove
#

Maybe first fix ur internet

#

Cuz its kinda laggy

#

Thats probably why the animations dont load

silk briar
#

no

#

what

#

my internet is fine

#

the script its just wrong

next grove
#

RunAnimation.Looped = true u can also set it in the animation editor

#

like looped

next grove
silk briar
#

from the end

#

like with the other ones

next grove
#

ur getting newstate how

silk briar
#

when the player humanoid state change

next grove
#

can u send it?

silk briar
#

ok wait

next grove
#

ye did u know it gives u an oldstate and a newstate?

vapid ridge
# silk briar

Once input detected, check if its the right keybind then play the looping animation, One input is done being held, then Stop and Destroy the Track.

silk briar
#

no

vapid ridge
#

Make sure the Track is Global

#

And not inside the function

#

✌️

silk briar
#

can u say in a way like uga buga for me cuz uhm

vapid ridge
silk briar
#

cuz english is not my man language

vapid ridge
#

once they stop clicking it

#

anim stops

silk briar
#

no

vapid ridge
#

boom

silk briar
#

i was doing that

#

the problem is that when the player jumps, climb, freefall and etc the animation doesnt stop

vapid ridge
#

Then check the states

silk briar
#

thats what im doing

vapid ridge
#

Make sure the animation is in Core

#

just saying

silk briar
#

like this

silk briar
next grove
lone steppeBOT
#

studio** You are now Level 16! **studio

next grove
#

and its oldstate and newstate

silk briar
#

ok i did a newstate and oldstate

next grove
#

nah its oldstate then newstate