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

Playing Animation when pressed key?

Hello, i am trying to make a game and i'm trying to make it that so my player plays an animation when i click example the left button. How am i supposed to do it?

Please help me!

2 answers

✓ Accepted answer

In StarterPlayer.StarterPlayerScripts add a LocalScript and add this

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")

-- Create new "Animation" instance
local Animation = Instance.new("Animation")
-- Set its "AnimationId" to the corresponding animation asset ID
Animation.AnimationId = "rbxassetid://6550418250"

-- Load animation onto the animator
local Animation = humanoid:LoadAnimation(Animation)

-- Play animation track
UserInputService.InputBegan:Connect(function(InputObject)
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		-- play animation
			Animation:Play()
			print("Playing animation?")
			wait(1) --Adjust Acordingly
			Animation:Stop()
	end
end)

You can use UserInputService, then see if the key is the left click button, then play the animation

https://developer.roblox.com/en-us/api-reference/class/UserInputService

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.MouseButton1 then -- another way for saying left click
		-- blah blah blah play animation
	end
end)

if you're talking about a gui then just use GuiButton.MouseButton1Click

4 comments

0

where do i put the script

0

and is it a local script

0

I would put it in StarterPlayer.StarterPlayerScripts

0

This would be a local script

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