Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make animations stop when not sprinting?

i tried using get state but it didnt work and the animation keeps playing when not sprinting i looked at the dev page on humanoid get state but i didnt quite understand it any help or pointers in the right direction would help?

local UIS = game:GetService("UserInputService")

local toggle = false

local player = game.Players.LocalPlayer

local Character = player.Character

UIS.InputBegan:Connect(function(input, state)
    if input.KeyCode == Enum.KeyCode.LeftControl then
		if state == Enum.HumanoidStateType.None
	then
		return
	end
        if toggle == false then
            toggle = true
            Character.Humanoid.WalkSpeed = 25
            local Anim = Instance.new("Animation")
            Anim.AnimationId = "rbxassetid://04981880688"
            local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
            PlayAnim:Play()
        else
            toggle = false
            Character.Humanoid.WalkSpeed = 16
            local Anim = Instance.new("Animation")
            Anim.AnimationId = "rbxassetid://04981880688"
            local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
            PlayAnim:Stop()
        end
    end
end)

1 answer

Improvements

Use :GetService() to retrieve the Players service

Since running is being controlled by an UIS input, the HumanoidStateType is not required.

Issues

When stopping the animation, you create a new animation, when instead you need to be tracking the animation that was loaded into the player when the toggle was turned on. If you load the animation beforehand, you can just call Play() and Stop() on the AnimationTrack without having to redefine the animation

Revised Local Script

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local toggle = false

-- making sure the character is in the game before preparing the animation
repeat
	wait()
until player.Character and player.Character.Parent == game:GetService("Workspace")

local Humanoid = player.Character:WaitForChild("Humanoid")

local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://04981880688"
local PlayAnim = Humanoid:LoadAnimation(Anim)

UIS.InputBegan:Connect(function(input, state)
	if input.KeyCode == Enum.KeyCode.LeftControl then		
		if toggle == false then
			toggle = true
			Humanoid.WalkSpeed = 25
			PlayAnim:Play()
		else
			toggle = false
			Humanoid.WalkSpeed = 16
			PlayAnim:Stop()
		end
	end
end)

Side Note: The player's WalkSpeed should be adjusted on the Server not the Client

This discussion is preserved as it was. New answers, comments, and votes are closed.