#I can't figure out how to play an animation when Sprinting
1 messages · Page 1 of 1 (latest)
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)```
You can use Humanoid.StateChanged
you can also detect when the player jumps then stop the animations
yea it looks decnt
yeah but when i jump after the animation it just stops and doesnt play anymore
theres enum.humanoidstatetype.landed
ok
i used a big part of it and just looks weird
i can run the animation whenever iw ant
Well when u jump set it to freefall?
Cool
Do u want me to fix it?
ok
yeah or idk help
Maybe first fix ur internet
Cuz its kinda laggy
Thats probably why the animations dont load
where are u calling this function from?
ur getting newstate how
when the player humanoid state change
can u send it?
ye did u know it gives u an oldstate and a newstate?
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.
no
what wait
can u say in a way like uga buga for me cuz uhm
once they click the shift button
anim plays
cuz english is not my man language
no
boom
i was doing that
the problem is that when the player jumps, climb, freefall and etc the animation doesnt stop
Then check the states
thats what im doing
like this
yes it is
so ur passing 1 argument instead of 2
** You are now Level 16! **
and its oldstate and newstate
ok i did a newstate and oldstate
