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!
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