How might I add an animation to this Script?

I’m having trouble with making an animation work with this shift to sprint script. I don’t know where to add it, and I would like it to where the animation stops when the player is not moving.

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 2

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		print(playerEntity.Name.. " humanoid's walkspeed has been set to " ..boostedSpeed)
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
		print(playerEntity.Name.. " humanoid's walkspeed has been set to " ..normalSpeed)
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

Inside of startSprinting you could make the animation Play
Inside of stopSprinting you stop the animation.
For the animation to stop while you’re not moving you can check if Humanoid.MoveDirection.magnitude == 0 and continue from there

wouldn’t the stop for the animation be better with humanoid.MoveDirection.magnitude > 0?

pretty sure thats what I said for when you’re standing still.
Im just saying that when you press the key to manually stop printing itll just stop the animation.
Maybe you could check if sprinting is false BEFORE checking if you’re standing still
(this sounds confusing)

so i’ve done this, and im yet again stumped. I have tried about 6-7 different solutions, and none have function properly. How would I play the animation? And where would I put the check to see if the player is not moving?
Here’s the code thus far (Without the failed solutions):

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
		print(playerEntity.Name.. " humanoid's walkspeed has been set to " ..normalSpeed)
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)

You didnt set the animation to Play. Add this under animation.AnimationId

local RunAnim = characterHumanoid:LoadAnimation(animation)
RunAnim:Play()

Do the same inside stopSprinting but instead of Play put Stop.

To check when the Player is standing still you can use RunService.Heartbeat then put your conditions inside of it
example
RunService.Heartbeat:Connect(function()
if characterHumanoid:MoveDirection.Magnitude == 0 then
for i,v in pairs(characterHumanoid:GetPlayingAnimationTracks()) do
if v.Name == (The animation instances actual name) do
v:Stop()
end
end
end
end)

Like this?

local PlayersService = game:GetService("Players")
local UIS = game:GetService("UserInputService")


local playerEntity = PlayersService.LocalPlayer
local character = playerEntity.Character or playerEntity.CharacterAdded:Wait()
local characterHumanoid = character:WaitForChild("Humanoid")

local normalSpeed = 16
local boostedSpeed = normalSpeed * 1.25

local function startSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = boostedSpeed
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
		local RunAnim = characterHumanoid:LoadAnimation(animation)
		RunAnim:Play()
	end
end

local function stopSprinting(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		characterHumanoid.WalkSpeed = normalSpeed
		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=9360403056"
		local RunAnim = characterHumanoid:LoadAnimation(animation)
		RunAnim:Stop()
	end
end

UIS.InputBegan:Connect(startSprinting)
UIS.InputEnded:Connect(stopSprinting)
1 Like