I have a left shift prompted running script here and I would like the character to play the running animation when shift is toggled and stop when released, however I’m having trouble with stopping the animation when shift is released.
Here’s the local script:
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
ReplicatedStorage.RemoteEvents.Sprint:FireServer("StartedState")
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
ReplicatedStorage.RemoteEvents.Sprint:FireServer("EndedState")
end
end)
ReplicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(Stamina,MaxStamina)
Players.LocalPlayer.PlayerGui.Stamina.StaminaBar.Size = UDim2.new(0.1,(Stamina/MaxStamina) * 410,0,31)
end)
Here’s the part of the script:
ReplicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(Player,State)
local Humanoid = Player.Character.Humanoid
local RunAnimation = Instance.new("Animation")
RunAnimation.AnimationId = 'rbxassetid://6059811537'
local LoadRunAnimation = Humanoid:LoadAnimation(RunAnimation)
LoadRunAnimation.Priority = Enum.AnimationPriority.Movement
if State == "StartedState" and Humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and Humanoid.MoveDirection.Magnitude > 0 then
SprintingPlayers[Player.Name] = Humanoid.WalkSpeed
Humanoid.WalkSpeed = Humanoid.WalkSpeed * SprintModifier
if SprintingPlayers[Player.Name] then
LoadRunAnimation:Play()
end
elseif
State == "EndedState" and SprintingPlayers[Player.Name] then
Humanoid.WalkSpeed = SprintingPlayers[Player.Name]
SprintingPlayers[Player.Name] = nil
end
if SprintingPlayers[Player.Name] == nil then
LoadRunAnimation:Destroy()
end
end)
I’ve tried placing debounces, more conditional statements but none worked. Should I even play the animation in this function of the script or in the heartbeat function that runs later down the script?