Melee Take Damage Issue

Hello, I’m making weapons similar to Item Asylum. My problem is I don’t know how to do the damage system.
I used part as hitbox and touch event. It worked but had a lot of bugs, e.g. hitbox did damage and sometimes it didn’t. Then I tried this: Raycast Hitbox 4.01: For all your melee needs! but It also had bug. It was detecting humanoid but it was not dealing damage, and it was selecting humanoids from other “Dummy”.

I don’t think it’s all because of the animations because “Hitboxes 4.0” was normally detecting a humanoid.

My question is how can I fix this? or how to do damage systems?

My code using roblox touch event:

wait(1)
-- // TweenService //
local TS = game:GetService("TweenService")
local TInfo = TweenInfo.new(1, Enum.EasingStyle.Elastic, Enum.EasingDirection.Out)
local T1 = TS:Create(script.Parent.Model, TInfo, { Transparency = 0 } )

-- // Player //
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

-- // Animations //
local IdleAnimation = script.Parent:WaitForChild("IdleAnimation")
local Attack1 = script.Parent:WaitForChild("Attack1")
local Attack2 = script.Parent:WaitForChild("Attack2")

local IdleTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(IdleAnimation)
local Attack1Track = Humanoid:WaitForChild("Animator"):LoadAnimation(Attack1)
local Attack2Track = Humanoid:WaitForChild("Animator"):LoadAnimation(Attack2)

-- // RemoteEvents //
local DamageTaken = script.Parent.Remote:WaitForChild("DamageTaken")
local ItemEquipped = script.Parent.Remote.ItemEquipped

-- // Values //
local Debounce = script.Parent.Config.Cooldown.Value
local CanSwing = true
local CanDealDamage = false
local Click = 0

-- // Equipped //
script.Parent.Equipped:Connect(function()
	script.Parent.Model.Transparency = 100
	ItemEquipped:FireServer(Character)
	T1:Play()
	IdleTrack:Play()
	script.Parent.Sounds.NEONCHAOS:Play()
end)

script.Parent.Unequipped:Connect(function()
	IdleTrack:Stop()
	script.Parent.Sounds.NEONCHAOS:Stop()
end)

-- // Activated //
script.Parent.Activated:Connect(function()
	if Click == 0 then -- Making if for alternating animations
		if CanSwing and CanDealDamage == false then
			CanDealDamage = true
			CanSwing = false
			
			script.Parent.Hitbox.Touched:Connect(function(Hit) -- Touch Event
				if CanDealDamage == true then
					CanDealDamage = false
					DamageTaken:FireServer(Hit, script.Parent)
				end
			end)
			
			IdleTrack:Stop()
			Attack1Track:Play()
			script.Parent.Sounds.Attack1:Play()
			script.Parent.Model.Trail.Enabled = true
			Attack1Track.Stopped:Wait()
			IdleTrack:Play()
			CanDealDamage = false
			script.Parent.Model.Trail.Enabled = false
			
			Click = math.clamp(Click+1, 0, 1)
			
			wait(Debounce)
			CanDealDamage = false
			CanSwing = true
		end
	elseif Click == 1 then
		if CanSwing and CanDealDamage == false then
			CanDealDamage = true
			CanSwing = false

			script.Parent.Hitbox.Touched:Connect(function(Hit) -- Touch Event
				if CanDealDamage == true then
					CanDealDamage = false
					DamageTaken:FireServer(Hit, script.Parent)
				end
			end)

			IdleTrack:Stop()
			Attack2Track:Play()
			script.Parent.Sounds.Attack1:Play()
			script.Parent.Model.Trail.Enabled = true
			Attack2Track.Stopped:Wait()
			IdleTrack:Play()
			CanDealDamage = false
			script.Parent.Model.Trail.Enabled = false

			Click = 0

			wait(Debounce)
			CanDealDamage = false
			CanSwing = true
		end
	end
end)

You can also review my code to give me coding tips or optimization :wink: