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)
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)
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)
Voila! I really hope I taught you something today. You're welcome! :)
~Sensei_Developer