Help with hitbox issue

So, whenever i swing my sword at the enemy and hit him it does damage, but it also does damage when he brings the sword back up to reset the animation. here is a video of what i mean. im not sure whats causing this.

2 Likes

It would be a lot easier to help if you could provide the code that you used.

But if I were in this situation, only let it damage the monster once before the animation is over.

Assuming its in a .Touched function:

UNTESTED CODE

-- implies animTrack is already a variable
local hits = {}
hitbox.Touched:Connect(function(hit)
       if (hit.Parent:FindFirstChild("Humanoid") then
             if table.find(hits, hit.Parent.Name) then return end
             local character = hit.Parent
             local hum = character.Humanoid
             table.insert(hits, character.Name)
             delay(animTrack.Length, function()
                    table.find(hits, character.Name) = nil
             end)
             -- Damage code
           
       end
end)

In your attack script (i guess it’s hitbox.Touched) you can use debonce and set certain delay for dealing damage

It was a commission that i ordered as im not good at this kind of thing but i t thin k ifound the portion of the code that is causing it

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

This is the full code right here. I tried to implement your code in there but i couldn’t get it right. I appreciate the help a lot.

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 hits = {}

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 attackEvent = Tool.Events:WaitForChild("Attack")
local returnEvent = Tool.Events:WaitForChild("ReturnData")

local slashSound = Tool.Sounds:WaitForChild("SwordSlash")
local sheathSound = Tool.Sounds:WaitForChild("sheath")
local unsheathSound = Tool.Sounds:WaitForChild("Unsheath")
local hitSound = Tool.Sounds:WaitForChild("hitSound")

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

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

firebrandHitbox.DetectionMode = RaycastHitbox.DetectionMode.PartMode

firebrandHitbox.OnHit:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if table.find(hits, hit.Parent.Name) then return end
		local character = hit.Parent
		local hum = character.Humanoid
		
		hitSound:Play()
		game.ReplicatedStorage.InflictDamage:FireServer(Tool.Name, hit.Parent.Name)
	elseif hit.Parent.Parent == workspace.Resources then
		hitSound:Play()
		game.ReplicatedStorage.BreakResource:FireServer(hit.Parent.Name)
	end
end)
Tool.Equipped:Connect(function()
	unsheathSound:Play()
	equipAnimation:Play()
	equipAnimation.Stopped:Wait()
	idleAnimation:Play()
end)

Tool.Unequipped:Connect(function()
	sheathSound:Play()
	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
		slashSound:Play()
		idleAnimation:Stop()
		animationIndex[num]:Play()
		firebrandHitbox:HitStart()
		animationIndex[num].Stopped:Wait()
		firebrandHitbox:HitStop()
		
		if check(player.Character) then
			idleAnimation:Play()
		end
	end
end)

This should work, lmk if it doesnt’

just replace lines 39 - 55 with:

firebrandHitbox.OnHit:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if table.find(hits, hit.Parent.Name) then return end
		local character = hit.Parent
		local hum = character.Humanoid

		hitSound:Play()
		table.insert(hits, character.Name)
		game.ReplicatedStorage.InflictDamage:FireServer(Tool.Name, hit.Parent.Name)
		delay(0.75, function()
			table.remove(hits, table.find(hits, character.Name))
		end)
	elseif hit.Parent.Parent == workspace.Resources then
		hitSound:Play()
		game.ReplicatedStorage.BreakResource:FireServer(hit.Parent.Name)
	end
end)

Thank you so much! It worked! I thought i was going to have to get a new system for a while.

1 Like