How to trigger an animation when pressing a button

Hello!

could anyone help me. i want a pushing animation to play when player presses a button, “e” for example

I already have an animation. now i just need the code to play this animation on the player when the player presses e

hope yall can help :))

also please try to explain what you are telling me, im kinda new in making games so im still learning.

ThanQ

i think it can help

You could try to use UserInputService if you know what that is.

local UserInputService = game:GetService("UserInputService")
local animation  = Instance.new("Animation", workspace)
animation.AnimationId = "rbxassetid://INSERTANIMATIONIDHERE"


UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		for i,v in pairs(workspace:GetDescendants()) do
			if v.Name ==  "Humanoid" then
				v:LoadAnimation(animation)
			end
		end
	end
end)

(This isn’t tested but its something like this!)

1 Like

I would do this


Put a local in starterCharacterScripts
And put an animation in there with your animationId

You don’t need to loop through the workspace:GetDescendants() for “Humanoid” when you can get the humanoid from the player.Character