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

Animation won't play when shift is pressed?

Hi, I made a sprint script and tried to make an animation play when I press shift. Only problem is, it won't play. I tried adjusting the priority and put it in different parts of the script but it doesn't work.

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2510238627"

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")

local track = human:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action

mouse.KeyDown:Connect(function(k)
	if k:lower():byte() == 48 then
		track:Play()
		human.WalkSpeed = 23
	end
end)

mouse.KeyUp:Connect(function(k)
	if k:lower():byte() == 48 then
		track:Stop()
		human.WalkSpeed = 16
	end
end)

1 answer

✓ Accepted answer

Hello. Try using UserInputService as Mouse.KeyDown is deprecated. Try this:

local UserInputService = game:GetService("UserInputService")

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2510238627"

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")

local track = human:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Action

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessedEvent then
		track:Play()
		human.WalkSpeed = 23
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessedEvent then
	       track:Stop()
		human.WalkSpeed = 16
	end
end)

The InputBegan function fires when an input is detected. Then there's an if statement checking if the input is left shift and that the player isn't in chat. Then it plays the animation and changes the walkspeed.

5 comments

0

It works, but my animation doesn't change. It's supposed to change my walking animation to the animation in the script.

0

If it matters I'm using the werewolf run animation

0

Try setting the animation's priority inside the animation editor.

0

Nevermind, the animation ID was incorrect.

0

oof

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