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

How do you make this tool loop it's animation while your left click button is being held?

Asked by 5 years ago

I was animating and scripting a tool for it to be a click-and-hold tool, which means it should loop the animation when you click and hold your left mouse button, but it should immediately stop once you release it. I got the animations done and everything, but I need the "looping" part. How do you do that?

Oh, and for some reason, the animation plays even if you don't have the tool equipped.

Here's the local script I'm using:

wait(.2)
local tool = script.Parent
local debounce = true
local player = game.Players.LocalPlayer
local character = player.Character
local idle
local attack
local music = script.Parent.Hitbox["Sound"]

tool.Equipped:Connect(function()
    idle = character.Humanoid:LoadAnimation(script.Parent.Idle)
    idle:Play()
end)

tool.Unequipped:Connect(function()
    idle:Stop()
end)

-- le animating

local InputService = game:GetService("UserInputService")

InputService.InputBegan:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        attack = character.Humanoid:LoadAnimation(script.Parent.Attack)
        attack:Play()
        music:Play()
        music.TimePosition = 0.5
    end
end)

InputService.InputEnded:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        attack = character.Humanoid:LoadAnimation(script.Parent.Attack)
        attack:Stop()
        music:Stop()
    end
end)

1 answer

Log in to vote
1
Answered by 5 years ago

Introduction

If you didn't know, there's a function of a Tool. That's called...

Activated and Deactivated.

What are they?

To put it simply, it's a function in which it detects when the player has clicked and un-clicked their mouse.

Tool.Activated:Connect(function()
    print("Mouse down")
end)

Tool.Deactivated:Connect(function()
    print("Mouse up")
end)

Procedures

With this, you can have an animation (that's looped, of course) that will play inside of the tool.

So, for the final procedures, follow these steps.

1) Insert a LocalScript inside of the Tool.

2) Get your looped animation ID and get the Humanoid defined as a variable.

3) With your variables, write this:

local Animation = Instance.new("Animation",script.Parent) --Creating and Animation and parenting it to the tool
Animation.AnimationId = 'rbxassetid://12345678' --< Your animation ID here
Animation.Name = "Whatever you want to name this"
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')

local Anim = Humanoid:LoadAnimation(Animation)

Tool.Activated:Connect(function()
    Anim:Play()
end)

Tool.Deactivated:Connect(function()
    Anim:Play()
end)

However, there is a flaw with this. When you unequip it, the animation keeps playing. So now, let's use the .Unequipped function.

local Animation = Instance.new("Animation",script.Parent) --Creating and Animation and parenting it to the tool
Animation.AnimationId = 'rbxassetid://12345678' --< Your animation ID here
Animation.Name = "Whatever you want to name this"
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')

local Anim = Humanoid:LoadAnimation(Animation)

Tool.Activated:Connect(function()
    Anim:Play()
end)

Tool.Deactivated:Connect(function()
    Anim:Play()
end)

Tool.Unequipped:Connect(function()
    Anim:Stop()
end)

Conclusion

Voila! I really hope I taught you something today. You're welcome! :)

~Sensei_Developer

0
How you make that "titles" I wanna know. I'm a scripter and I make a explanation too. So this will be really helpful. Foxy_Developer 111 — 5y
0
Oh! You just have to do on the editor a #**Text** Sensei_Developer 298 — 5y
0
There was an error. The script doesn't know what game.Players.LocalPlayer.Character was I think. comfycouchman 0 — 5y
0
Nevermind, fixed it. comfycouchman 0 — 5y
View all comments (2 more)
0
Have a nice day ;) Sensei_Developer 298 — 5y
0
Also, accept my answer please. Sensei_Developer 298 — 5y
Ad

Answer this question