So I’m making a item where when you click the animation plays for the item, But when you click multiple times the animation restarts from the beginning of the animation.
for example when I click once the animation plays, but when you spam your mouse button the animation keeps restarting to the start of the animation.
Any help would be great!
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
animation:Play()
end)
end)
script.Parent.Unequipped:Connect(function()
if animation ~= nil then
animation:Stop()
end
end)
Forgot to mention, I’m trying to make it so that when you click one time the animation only plays once and when you click multiple times it will keep playing that animation instead of restarting.
local D = true
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
if D == true then
D = false
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
animation:Play()
animation.Stopped:Wait()
D = true
end
end)
end)
script.Parent.Unequipped:Connect(function()
if animation ~= nil then
animation:Stop()
D = true
end
end)
The article shows you that you can do the following:
YourAnimation.Stopped:wait()
I’m not exactly sure what you want to happen, but if you want to not allow the player to click unless the animation has finished I would definitely do what @luisgamercooI231fan suggested.
local D = true
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
if D == true then
D = false
animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
animation:Play()
end
end)
end)
script.Parent.Unequipped:Connect(function()
if animation ~= nil then
animation:Stop()
end
end)
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = script.Parent
local animation = tool:WaitForChild("Animation")
local track = humanoid:LoadAnimation(animation)
tool.Unequipped:Connect(function()
if animation.IsPlaying then
animation:Stop()
end
end)
tool.Activated:Connect(function()
if animation.IsPlaying then
return
end
animation:Play()
end)
I know you’re just fixing bugs but it’s best to provide better solutions.
Regarding the thread’s poster, don’t forget that the “.Activated” event of tool instances exists (it is fired on a tool whenever the player presses the left mouse button while that tool is equipped).
There is no need for a debounce here and using state variables/flags should be avoided when possible.