Help with animations

Hey everyone. Hope all is well. I have an issue with my animations, the script is supposed to let the attack animation finish before allowing the player to perform the next attack. That part works fine. The issue is that once the animation is done playing he stands idle for about a second before performing the next attack. So once i’ve done my sword swing my avatar just stand there for a second before allowing me to swing again.

local player = game:GetService("Players").LocalPlayer
local Tool = script.Parent.Parent

local RaycastHitbox = require(Tool.Scripts:WaitForChild("RaycastHitboxV4"))

local hum = player.Character:WaitForChild("Humanoid")
local Animator = hum:WaitForChild("Animator")

local idleAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("Idle"))
local equipAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("equip"))
local unequipAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("unequip"))

local swing1Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing1"))
local swing2Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing2"))
local swing3Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing3"))
local swing4Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing4"))
local swing5Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing5"))

local equipEvent = Tool.Events:WaitForChild("Equip")
local attackEvent = Tool.Events:WaitForChild("Attack")
local returnEvent = Tool.Events:WaitForChild("ReturnData")

local swordHitbox = RaycastHitbox.new(script.Parent)
swordHitbox:LinkAttachments(Tool.Handle.Attachment1, Tool.Handle.Attachment2)

local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {player.Character}
RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
swordHitbox.RaycastParams = RaycastParams
swordHitbox.Visualizer = false

swordHitbox.DetectionMode = RaycastHitbox.DetectionMode.PartMode

swordHitbox.OnHit:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		game.ReplicatedStorage.InflictDamage:FireServer(Tool.Name, hit.Parent.Name)
	elseif hit.Parent.Parent == workspace.Resources then
		game.ReplicatedStorage.BreakResource:FireServer(hit.Parent.Name)
	end
end)

Tool.Equipped:Connect(function()
	equipAnimation:Play()
	equipAnimation.Stopped:Wait()
	idleAnimation:Play()
	equipEvent:FireServer("Equip")
end)

Tool.Unequipped:Connect(function()
	equipEvent:FireServer("Unequip")
	idleAnimation:Stop()
	unequipAnimation:Play()
end)

Tool.Activated:Connect(function()
	attackEvent:FireServer()
end)

local function check(char)
	for i, v in ipairs(char:GetChildren()) do
		if v:IsA("Tool") and Tool == v then
			return true
		end
	end
end

--first value in table is animation length and the second is the animation

local animationIndex = {
	["1"] = swing1Animation,
	["2"] = swing2Animation,
	["3"] = swing3Animation,
	["4"] = swing4Animation,
	["5"] = swing5Animation
}

returnEvent.OnClientEvent:Connect(function(num)
	if check(player.Character) then
		idleAnimation:Stop()
		animationIndex[num]:Play()
		
		swordHitbox:HitStart()

		animationIndex[num].Stopped:Wait()
		swordHitbox:HitStop()

		if check(player.Character) then
			idleAnimation:Play()
		end
	end
end)

Can you tell us which part of the script does that so we can observe the problem easier?

1 Like
returnEvent.OnClientEvent:Connect(function(num)
	if check(player.Character) then
		idleAnimation:Stop()
		animationIndex[num]:Play()
		
		swordHitbox:HitStart()

		animationIndex[num].Stopped:Wait()
		swordHitbox:HitStop()

		if check(player.Character) then
			idleAnimation:Play()
		end
	end
end)

.stopped:wait() is the correct way to do this?

1 Like