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:

01wait(.2)
02local tool = script.Parent
03local debounce = true
04local player = game.Players.LocalPlayer
05local character = player.Character
06local idle
07local attack
08local music = script.Parent.Hitbox["Sound"]
09 
10tool.Equipped:Connect(function()
11    idle = character.Humanoid:LoadAnimation(script.Parent.Idle)
12    idle:Play()
13end)
14 
15tool.Unequipped:Connect(function()
View all 38 lines...

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.

1Tool.Activated:Connect(function()
2    print("Mouse down")
3end)
4 
5Tool.Deactivated:Connect(function()
6    print("Mouse up")
7end)

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:

01local Animation = Instance.new("Animation",script.Parent) --Creating and Animation and parenting it to the tool
02Animation.AnimationId = 'rbxassetid://12345678' --< Your animation ID here
03Animation.Name = "Whatever you want to name this"
04local Humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')
05 
06local Anim = Humanoid:LoadAnimation(Animation)
07 
08Tool.Activated:Connect(function()
09    Anim:Play()
10end)
11 
12Tool.Deactivated:Connect(function()
13    Anim:Play()
14end)

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

01local Animation = Instance.new("Animation",script.Parent) --Creating and Animation and parenting it to the tool
02Animation.AnimationId = 'rbxassetid://12345678' --< Your animation ID here
03Animation.Name = "Whatever you want to name this"
04local Humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid')
05 
06local Anim = Humanoid:LoadAnimation(Animation)
07 
08Tool.Activated:Connect(function()
09    Anim:Play()
10end)
11 
12Tool.Deactivated:Connect(function()
13    Anim:Play()
14end)
15 
16Tool.Unequipped:Connect(function()
17    Anim:Stop()
18end)

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