Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Playing Animation when pressed key?

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago

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)

Ad
Log in to vote
1
Answered by 3 years ago

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

0
where do i put the script OreyTheOre 52 — 3y
0
and is it a local script OreyTheOre 52 — 3y
0
I would put it in StarterPlayer.StarterPlayerScripts 8Bit_Taz 40 — 3y
0
This would be a local script Omq_ItzJasmin 666 — 3y

Answer this question