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

How to play animation on click with tool?

Asked by
jbm_pl 4
3 years ago
Edited 3 years ago

I need help because im trying to create Weight Lifting Simulator game And my code is

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Animation = script.Parent.OnClick

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        Animation:Play()
    end
end)
0
This is not a request site. DevingDev 346 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

WHAT IS A TOOL?

Tools are objects that a Humanoid object can equip. For players, they are stored in a Backpack object parented to a Player object.

In-game, players may have multiple tools which appear as icons at the bottom of the screen.

Equipping a tool moves it from the Backpack and into a player’s Character|character model in the Workspace. By default, tools are held in the right hand and have a handle in them, which is a Part named “Handle” inside (though one is not required if Tool.RequiresHandle is off). Tools that are to be provided to (re)spawning players ought to be stored in the StarterPack.

On desktop, pressing a number key (1, 2, 3…) will equip a tool. Equipped tools can be dropped into the Workspace by pressing Backspace.

It’s recommended that you turn Tool.CanBeDropped off so it is not possible to drop a tool, die, respawn and drop again to duplicate tools. On gamepads, LB and RB buttons will equip tools. You can disable activation via left click (or right trigger on gamepad) by setting Tool.ManualActivationOnly on. Doing so requires that you call Activate yourself through some sort of other user input.

Tools are not the only way to capture user input.

You can also use ContextActionService, UserInputService or Player:GetMouse If you need a Tool to have multiple actions, such as pressing a key while the Tool is equipped.

You should use ContextActionService’s BindAction and UnbindAction in the Equipped and Unequipped events, respectively. Use a LocalScript send these actions to the server via. a RemoteFunction inside the Tool.

-- Some of it from the wiki

Basic Tool Functions;

.Activated .Equipped .Unequipped .Deactivated

All of these samples are events, which are most commonly used, or another-words always used in tool events;

WHAT EVENT YOU NEED;

The Activated event fires when the player clicks while a Tool is equipped.

This function is used to perform an action when the player uses the tool. For instance, when the player clicks while a Rocket Launcher tool is equipped, the activated event executes the code to create and launch a rocket.

TOOL; CODE SAMPLE;

Insert a script in the Tool, paste the script below which will determine the current state;

local Tool = script.Parent

Tool.Activated:Connect(function()
    print("I was clicked!")
end)

WHAT IS AN ANIMATION?

Animations truly bring a game to life. From easily-accessible character animations in the catalog to detailed animations built with the Animation Editor, Roblox offers a variety of powerful animation options.


By default, Roblox player characters include common animations like running, climbing, swimming, and jumping. However, these animations are not locked in place — if desired, you can replace them with catalog animations or even load in your own custom animations.

CUSTOM ANIMATIONS

To use a custom animation exported from the Animation Editor, locate its asset ID as follows:

  1. Open the Animations section of the Create page.
  2. Locate the desired animation and click it.
  3. Copy its ID from the URL in your browser.

Load Animation

Controls the playback of an animation on a Humanoid or AnimationController. This object cannot be created, instead it is returned by the Humanoid:LoadAnimation method.

SCRIPT;

I'm assuming you already have your animation; Insert a local-script in your tool;

local Tool = script.Parent
local Player = game.Players.LocalPlayer

repeat wait() until Player.Character ~= nil

function Activate()
    local Animation = script.Parent.OnClick

    if Player.Character then
        local Character = Player.Character

        if Character:FindFirstChild("Humanoid") then
            local Hum = Character.Humanoid

            local AnimationTrack = Hum:LoadAnimation(Animation)
            AnimationTrack.Priority = Enum.AnimationPriority.Action

            AnimationTrack:Play()

        end
    end
end

Tool.Activated:Connect(Activate)
Ad
Log in to vote
2
Answered by 3 years ago

Your script is mostly correct, except you don't define what humanoid you're playing the animation on.

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Animation = script.Parent.OnClick
local animTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation)

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        animTrack:Play()
    end
end)

Please note that this script will only work from a local script.

0
I know this question is over a year old but, pretty sure that wouldn't work, as the script would run before the character. So essentially the character would be nil. JesseSong 3916 — 2y
0
@JesseSong Wrote this when I was stupid lol Cynical_Innovation 595 — 2y

Answer this question