What is the best way to script a tool run animation?

So I was trying all day to try and script a running animation for a tool, but the problem is I don’t know how I should do the Input or if I even should use Input(Began/Changed/Ended) for it.

Like for example right I wanted to have a sword equipped then have a nice and smooth run animation for it.

How should I script it?

I did have make some scripts for it but I deleted it because It wouldn’t work how I wanted to, should I use Enum.KeyCode? or something else for it because at this point I actually have no clue what I should do. I can easily change the default run/walk animations that are automatically inserted into the character when created. I can do that but I want to do that with a tool instead, if it’s too hard then of course I would just do something else and learn how to do it later but learning now is what I think would be better…

Here’s a sample of my previous code:

--//Variables
local Weapon = script.Parent
local Client = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

--//Events
Weapon.Equipped:Connect(function(mouse)
      local Character = player.Character
      local Humanoid = Character:WaitForChild("Humanoid")
      local runanimation = Humanoid:LoadAnimation(script.Parent.RunAnimation)
      UserInputService.InputBegan(function(input, gameProccessed)
             if not gameProccessed then
               if input.KeyCode == Enum.KeyCode.LeftShift and (input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D) then
                        runanimation:Play()
                      end 
               end

        
       end)
end)

--I forgot the rest but hopefully you get the point 

:smile:
To be honest I don’t even know if this part works correctly since it’s been a few hours since I made this…
So yeah I don’t really know what to do for this so some help would be really great THANKS!

2 Likes

It’s pretty much correct except for one issues: you’re never disconnecting the InputBegan connection that you make every time the tool is equipped. You can do that like so:

Weapon.Equipped:Connect(function(mouse)
    local Character = player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
    local runanimation = Humanoid:LoadAnimation(script.Parent.RunAnimation)

    local inputBeganConnection, unequippedConnection

    inputBeganConnection = UserInputService.InputBegan(function(input, gameProccessed)
        if not gameProccessed then
            if input.KeyCode == Enum.KeyCode.LeftShift and (input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D) then
                runanimation:Play()
            end
        end
    end)

    unequippedConnection = Weapon.Unequipped:Connect(function()
    	inputBeganConnection:Disconnect()
    	unequippedConnection:Disconnect()
    end)
end)

EDIT: Oh, and you should probably make it so that the animation stops when the player stops holding SHIFT. Let me know if you need help with that

2 Likes