I tried to make a script so that the player has a tool that when equipped it plays a combat animation, and when clicked, a punch animation plays.
but the Click Animation doesn’t stop playing
I tried changing the script and looking at some solutions on the internet, but nothing works.
I’m not sure if the problem is the script or the animations, but the “Hold Animation” is in a loop and the animation priority for both is “Action” and both have only one frame
there is the script →
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local player = players.LocalPlayer
local tool = script.Parent
local remotes = replicatedStorage:WaitForChild("Remotes")
local punchHoldAnimation = nil
local punchClickAnimation = nil
local debounce = false
local equipped = false
tool.Equipped:Connect(function()
local character = player.Character
if character == nil then
return
end
equipped = true
local humanoid = character:FindFirstChild("Humanoid")
if humanoid == nil then
return
end
local animator = humanoid:FindFirstChild("Animator")
if animator == nil then
return
end
punchHoldAnimation = animator:LoadAnimation(tool.Animations.PunchHold)
punchHoldAnimation:Play()
end)
tool.Unequipped:Connect(function()
equipped = false
if punchHoldAnimation == nil then
return
end
punchHoldAnimation:Stop()
end)
tool.Activated:Connect(function()
if debounce then
return
end
local character = player.Character
if character == nil then
return
end
if punchHoldAnimation ~= nil then
punchHoldAnimation:Stop()
end
debounce = true
local humanoid = character:FindFirstChild("Humanoid")
if humanoid == nil then
return
end
local animator = humanoid:FindFirstChild("Animator")
if animator == nil then
return
end
punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)
punchClickAnimation:Play()
remotes.Punch:FireServer()
punchClickAnimation.Stopped:Connect(function()
if punchHoldAnimation ~= nil and equipped then
punchHoldAnimation:Play()
end
debounce = false
end)
end)
You should probably load the animation when adding the variable(local). The value probably is still nil outside of Tool.Equippped because it only changed in Tool.Equipped function.
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local punchHoldAnimation = script.Parent.Animations.PunchHold
local punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)
(in this case it would be written below “local remotes”)
I have changed your code a little to make it easier to read and more readable (I hope you won’t be offended)
Script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local player = players.LocalPlayer
local tool = script.Parent
local remotes = replicatedStorage:WaitForChild("Remotes")
local punchHoldAnimation = nil
local punchClickAnimation = nil
local debounce = false
local equipped = false
tool.Equipped:Connect(function()
local character = player.Character
if not character then return end
equipped = true
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
local animator = humanoid:FindFirstChild("Animator")
if not animator then return end
punchHoldAnimation = animator:LoadAnimation(tool.Animations.PunchHold)
punchHoldAnimation.Looped = true
punchHoldAnimation:Play()
end)
tool.Unequipped:Connect(function()
equipped = false
if punchHoldAnimation then
punchHoldAnimation:Stop()
end
end)
tool.Activated:Connect(function()
if debounce then return end
local character = player.Character
if not character then return end
debounce = true
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
local animator = humanoid:FindFirstChild("Animator")
if not animator then return end
if punchHoldAnimation then
punchHoldAnimation:Stop()
end
punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)
punchClickAnimation:Play()
remotes.Punch:FireServer()
task.wait(punchClickAnimation.Length)
if punchHoldAnimation and equipped then
punchHoldAnimation:Play()
end
debounce = false
end)