Hitbox not working

im using this hitbox system https://www.youtube.com/watch?v=kXXYEo3cXCY and the hit function isn’t working right, it detects one thing touching it but then stops forever

local module = require(game.ServerStorage.MuchachoHitbox)

local hitbox = module.CreateHitbox()
hitbox.Size = Vector3.new(10, 10, 10)
hitbox.CFrame = CFrame.new(0, 5, 15)

local hitplrs = {}

hitbox:Start()

hitbox.Touched:Connect(function(hit)
	print(hit)
	if hit.Name == "HurtBox" then
		if table.find(hitplrs, hit.Parent.Name) then return end
		table.insert(hitplrs, hit.Parent.Name);
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
	end
end)

I put a hurtbox inside the player but if any part of the player is hit before the hurtbox then it breaks and stops doing anything.

The reason for this is the condition on line 14. After the first instance in which a player has been registered in hitplrs, the code will never reach line 16 for that player.

that’s not the problem, the player only get’s registered to hitplrs if the hitbox hit’s their hurtbox but it still stops working when the player’s character touches the hitbox before their hurtbox. it just doesn’t detect anything after the first hit.

In that case, I’m not sure if the problem lies in the code you’ve shown here

it does, i’ve used code like this before and it always worked, i mean this is the first time i’ve used this system so i might go back to something else

If you’ve used code like this before and it always worked, doesn’t that mean the problem lies elsewhere? Unless I’ve misinterpreted your writing.

In Default mode, it gets blocked by the Humanoid internally. or at least that’s what I think. So the Touched event doesn’t fire the way you’d expect.
There’s a HitParts mode, which would be more suitable in your case.

hitbox.DetectionMode = "HitParts" 

local hitplrs = {}
hitbox:Start()
hitbox.Touched:Connect(function(hit)
	if hit.Name == "HurtBox" then
		local character = hit.Parent
		if character and character:FindFirstChild("Humanoid") then
			if table.find(hitplrs, character.Name) then return end
			table.insert(hitplrs, character.Name)
			character.Humanoid.Health = character.Humanoid.Health - 10
			print(hit.Name)
		end
	end
end)
1 Like