Problem With: Trying to Trigger an Animation in a script

Hello Dev’s,

Today I ran in to a problem when making a running animation for my game. I made a running animation to play when you clicked (W) two times. I have the script to allow the speed to happen when the button is clicked but I don’t know how I can add in the script to have the animation play when I press the (W) key twice. I would love any help on this matter, Thanks again!

    local UIS = game:GetService("UserInputService")
local DefaultFOV = 70

local lastTime = tick()

local char = game:GetService("Players").LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.W then
		local now = tick()
		local difference = (now - lastTime)

		if difference <= 0.5 then
			if hum.WalkSpeed < 11 then
				print("running")
				hum.WalkSpeed = 35

				local properties = {FieldOfView = DefaultFOV + 15}
				local Info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0.1)
				local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
				T:Play()
			end
		end
		lastTime = tick()
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.W then

		print("running")
		hum.WalkSpeed = 10

		local properties = {FieldOfView = DefaultFOV}
		local Info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0.1)
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
		T:Play()

	end

end)

hum.Died:Connect(function()
	game.Workspace.Camera.FieldOfView = DefaultFOV
	hum.WalkSpeed = 10
	script.Disabled = true
end)

The code here allows me to increase my walk speed when the (W) key is pressed 2 times, but I want to add in where it plays a animation as well, any help would be greatly appreciated!

First, create an animation instance somewhere, and paste in the id.
To play an animation, you need to load the animation.

local anim = (where the location of the animation is)
local animator = hum:WaitForChild('Animator')
local track = animator:LoadAnimation(anim)
track:Play()

You would paste all of the variables at the top so the animation is loaded, and you would do the :Play() function when they have pressed w.

1 Like

What would I put for the location if the animation slot?

1 Like

Well, you would put in wherever your animation instance is at. For example if you created the animation in workspace, you would do:

local anim = workspace:WaitForChild('Anim')

you can also use the script.Parent:WaitForChild(“Anim”) method.

1 Like

Ok, so I did what you told me to but, it’s still not playing and I also get no output errors.

There appears to be some good tutorials about this topic on YouTube. In case you don’t find the answer here, I’d recommend checking out one of those and integrate their code into yours!

1 Like

Hey thanks I think I’m on to something!

1 Like