How to make punch tool?

Hello ! I want to make a tool that will play animation only once player click the screen with equiped tool . But my works like when you click , it play while I unequip tool or if I’ll click a few times that will never stop playing animation . Can you help with it please ?
Here the script from tool

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
wait(0.5)
    end)
end)

script.Parent.Unequipped:Connect(function()
        animation:Stop()
end)

robloxapp-20200529-1735525.wmv (2.8 MB)
Thank you !

Try defining you Variable animation in both functions.

1 Like

Couldn’t you define the variable in the equipped part instead as then it can be used for both.

2 Likes
script.Parent.Equipped:Connect(function(Mouse)
    animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
    Mouse.Button1Down:Connect(function()
        animation:Play()
        wait(0.5)
       animation:Stop()
    end)
end)

script.Parent.Unequipped:Connect(function()
        animation:Stop()
end)

Try this

1 Like

This works , but don’t you know how to avoid reanimation . Like this robloxapp-20200529-1838102.wmv (2.4 MB) When you click many times animation is bugging and replays

Ah yes, so you’d need to add a debounce in.

Basically it stops the player from doing the thing more than once while the variable is true.

local Debounce = false

script.Parent.Equipped:Connect(function(Mouse)
	animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
		    animation:Play()
			wait(0.5)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)

Give it a go, it should work.

1 Like

I don’t know but animation now doesn’t work at all . With previous script it worked but with repeat of animation when click a few times . Anyways thank you ! I’ll try to correct that .

Oh wait did you copy the local bit, I forgot to add it into the script markup bit.

local Debounce = false

script.Parent.Equipped:Connect(function(Mouse)
	animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
		    animation:Play()
			wait(0.5)
			animation:Stop()
			wait(0.1)
			Debounce = false
		end
    end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)
2 Likes

Yes , now it works ! Thank you very much !

1 Like