Tool animations wont play

I’m trying to load the animations from a folder called ‘anims’ located inside of the tool.

Everything looks fine to my knowledge, but for some reason when I click my mouse to perform an attack animation, nothing happens.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = nil

local swingTracks = {}
local bladeSensor
local isAttacking = false
local currentAnimationIndex = 1

-- Function to load animations from the tool's anims folder
local function loadAnimations(humanoid, tool)
	local animationTracks = {}
	local animsFolder = tool:FindFirstChild("anims")
	if animsFolder then
		for _, anim in ipairs(animsFolder:GetChildren()) do
			if anim:IsA("Animation") then
				local swingTrack = humanoid:LoadAnimation(anim)
				table.insert(animationTracks, swingTrack)
			end
		end
	else
		warn("No 'anims' folder found in tool")
	end
	return animationTracks
end

local function onActivate()
	if isAttacking then
		return -- Do not allow another attack while one is in progress
	end

	isAttacking = true

	if #swingTracks > 0 then
		local swingTrack = swingTracks[currentAnimationIndex]

		if swingTrack and swingTrack.IsPlaying then
			swingTrack:Stop()
		end
		if swingTrack then
			swingTrack:Play()

			local swingSound = Instance.new("Sound", tool.Handle)
			swingSound.SoundId = "rbxassetid://11802492693"
			swingSound:Play()
		end
	end
end

local function handleToolEquipped()
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		swingTracks = loadAnimations(player.Character.Humanoid, tool)

		-- Add event listener for the animation event to deal damage
		for _, swingTrack in ipairs(swingTracks) do
			swingTrack:GetMarkerReachedSignal("DealDamage"):Connect(function()
				local function checkRegionForEnemies()
					if bladeSensor then
						local regionSize = Vector3.new(5, 5, 5) -- Define the size of the region
						local regionCFrame = bladeSensor.CFrame
						local region = Region3.new(
							regionCFrame.Position - regionSize / 2,
							regionCFrame.Position + regionSize / 2
						)
						region = region:ExpandToGrid(4) -- Ensure the region size is a multiple of 4

						-- Visualize the region for debugging
						local part = Instance.new("Part")
						part.Size = regionSize
						part.Anchored = true
						part.CanCollide = false
						part.Material = Enum.Material.Neon
						part.BrickColor = BrickColor.new("Bright blue")
						part.CFrame = regionCFrame
						part.Transparency = 1 -- Set transparency to 1 (completely transparent)
						part.Parent = workspace
						game.Debris:AddItem(part, 0.1)

						-- Check if any enemies are within the region
						local foundEnemies = {}
						for _, instance in ipairs(workspace:FindPartsInRegion3(region, player.Character, math.huge)) do
							if instance.Name == "Hitbox" and instance.Parent:FindFirstChild("Humanoid") then
								table.insert(foundEnemies, instance.Parent.Humanoid)
							end
						end

						return foundEnemies
					end
					return {}
				end

				-- Check the region repeatedly during the animation
				local connection
				connection = game:GetService("RunService").RenderStepped:Connect(function()
					if swingTrack.IsPlaying then
						local enemiesHit = checkRegionForEnemies()
						for _, humanoid in ipairs(enemiesHit) do
							print("Enemy hit detected")
							-- Hit the enemy
							game.ReplicatedStorage.DamageEnemy:FireServer(humanoid, tool)
						end
						if #enemiesHit > 0 then
							connection:Disconnect()
						end
					else
						connection:Disconnect()
					end
				end)
			end)

			-- Add event listener to reset attack flag and advance animation index when animation ends
			swingTrack.Stopped:Connect(function()
				isAttacking = false
				currentAnimationIndex = currentAnimationIndex % #swingTracks + 1 -- Move to the next animation in the sequence
			end)
		end
	end
end

tool.Equipped:Connect(function()
	bladeSensor = tool:FindFirstChild("BladeSensor", true)
	handleToolEquipped()
end)

tool.Activated:Connect(onActivate)

I’m sure I didn’t have to post the full script, but I just cant figure out where the error is occurring.

3 Likes