Weapon Animations overriding each other

As shown in the video, the animations are just overriding each other for some reason. Each animation is set to its correct and respective priority. Not only that but it looks like whichever animation is currently playing doesn’t end as soon as a new one starts. the current animation will continue to play for a few frames before the new one starts. I’m not sure if the issue is happening in this script: (Animation ID’s were left out on purpose)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SwordModule = {}

local animations = {
	Idle = "rbxassetid://",
	Run = "rbxassetid://",
	Attack1 = "rbxassetid://",
	Attack2 = "rbxassetid://",
	Attack3 = "rbxassetid://",
	Attack4 = "rbxassetid://",
	Attack5 = "rbxassetid://",
	Attack6 = "rbxassetid://"
}

local swingTracks = {}
local idleTrack
local runTrack
local isAttacking = false
local currentAnimationIndex = 1
local runningConnection
local stateConnection

local function stopAllAnimations()
	if idleTrack then idleTrack:Stop() end
	if runTrack then runTrack:Stop() end
	for _, track in pairs(swingTracks) do
		track:Stop()
	end
end

function SwordModule:InitializeSword(player, tool)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	if humanoid then
		-- Load animations using Instance.new correctly
		local idleAnimation = Instance.new("Animation")
		idleAnimation.AnimationId = animations.Idle
		idleTrack = humanoid:LoadAnimation(idleAnimation)

		local runAnimation = Instance.new("Animation")
		runAnimation.AnimationId = animations.Run
		runTrack = humanoid:LoadAnimation(runAnimation)

		-- Load attack animations correctly
		for _, animationId in pairs({animations.Attack1, animations.Attack2, animations.Attack3, animations.Attack4, animations.Attack5, animations.Attack6}) do
			local attackAnimation = Instance.new("Animation")
			attackAnimation.AnimationId = animationId
			table.insert(swingTracks, humanoid:LoadAnimation(attackAnimation))
		end

		-- Start idle animation
		idleTrack:Play()

		-- Manage running
		runningConnection = humanoid.Running:Connect(function(speed)
			if speed > 0 and not isAttacking then
				stopAllAnimations()
				runTrack:Play()
			else
				stopAllAnimations()
				idleTrack:Play()
			end
		end)

		-- Manage state changes
		stateConnection = humanoid.StateChanged:Connect(function(_, newState)
			if newState == Enum.HumanoidStateType.Freefall or newState == Enum.HumanoidStateType.Jumping then
				stopAllAnimations()
			elseif newState == Enum.HumanoidStateType.Landed then
				if humanoid.MoveDirection.Magnitude > 0 then
					runTrack:Play()
				else
					idleTrack:Play()
				end
			end
		end)

		-- Connect attack logic
		tool.Activated:Connect(function()
			SwordModule:PerformAttack(player)
		end)
	end
end

function SwordModule:PerformAttack(player)
	local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid and not isAttacking then
		isAttacking = true
		humanoid.WalkSpeed = 0
		stopAllAnimations()
		swingTracks[currentAnimationIndex]:Play()
		swingTracks[currentAnimationIndex].Stopped:Wait()
		humanoid.WalkSpeed = 16
		currentAnimationIndex = (currentAnimationIndex % #swingTracks) + 1
		isAttacking = false
		idleTrack:Play()
	end
end

function SwordModule:StopSwordAnimations()
	stopAllAnimations()
	if runningConnection then runningConnection:Disconnect() end
	if stateConnection then stateConnection:Disconnect() end
end

return SwordModule

or this one:

local ServerScriptService = game:GetService("ServerScriptService")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local swordModule = require(ServerScriptService:WaitForChild("Weapon Scripts"):WaitForChild("Sword"):WaitForChild("SwordModule"))

local function onToolEquipped(player, tool)
	if CollectionService:HasTag(tool, "Swords") then
		print(player.Name .. " equipped a sword!")
		swordModule:InitializeSword(player, tool)
	end
end

local function onToolUnequipped(player, tool)
	if CollectionService:HasTag(tool, "Swords") then
		swordModule:StopSwordAnimations(player)
	end
end

local function monitorPlayerTools(player)
	player.CharacterAdded:Connect(function(character)
		for _, tool in pairs(player.Backpack:GetChildren()) do
			if tool:IsA("Tool") then
				tool.Equipped:Connect(function()
					onToolEquipped(player, tool)
				end)
				tool.Unequipped:Connect(function()
					onToolUnequipped(player, tool)
				end)
			end
		end
		character.ChildAdded:Connect(function(child)
			if child:IsA("Tool") then
				child.Equipped:Connect(function()
					onToolEquipped(player, child)
				end)
				child.Unequipped:Connect(function()
					onToolUnequipped(player, child)
				end)
			end
		end)
	end)
end

Players.PlayerAdded:Connect(monitorPlayerTools)
print("WeaponHandler initialized and ready!")

any help is appreciated.

1 Like