Same humanoid being detected and damaged multiple times

Im making a fighting game and i am using my own hit box system. It uses overlaps and then searches the returned parts for humanoids to damage. Ive tried making it detect if the humanoid is already inside the detected humanoids table, but it still ends up detecting the same humanoid.

Hitbox function:

function HitboxModule.Box(CF:CFrame, Size:Vector3, Blacklist:{Instance}?)
	local Params = OverlapParams.new()
	if Blacklist then
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params.FilterDescendantsInstances = Blacklist
	end
	
	local BoundsCheck = workspace:GetPartBoundsInBox(CF, Size, Params)
	return BoundsCheck
end

Function to search for humanoids in a list of instances:

function HitboxModule.GetHumanoids(Instances:{Instance})
	local Humanoids = {}
	
	for i, instance in pairs(Instances) do
		local Humanoid = instance.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			if not table.find(Humanoids, Humanoid) then
				Humanoids[i] = Humanoid
			end
		end
	end
	
	print(Humanoids)
	return Humanoids
end

Before i added the table.find() check, it would damage the same Humanoid 6 times (Because of its 6 parts). Now, if theres two humanoids together, it will damage one once and the other 7 times. I have no idea why this is happening and i cant find anything similar that other people have talked about.

For extra info, i have a custom damage system being used.

Here:

local PartsHit = HitboxModule.Box(HitboxCFrame, Vector3.one*3)
				--[[local Part = Instance.new("Part", workspace)
				Part.CanCollide = false
				Part.Anchored = true
				Part.Transparency = .5
				Part.CFrame = HitboxCFrame
				Part.Size = Vector3.one*3--]]
				local Humanoids = HitboxModule.GetHumanoids(PartsHit)
				for i, Humanoid in pairs(Humanoids) do
					local Char = Humanoid.Parent
					local StatManager = require(Char.StatManager)
					
					local AttackInfo = {}
					AttackInfo.Damage = 10
					AttackInfo.Knockback = (HumanoidRootPart.CFrame * CFrame.Angles(math.rad(45), 0, 0)).LookVector * 35
					AttackInfo.Stun = .85
					AttackInfo.Ragdoll = true
					
					local Return = StatManager.TakeDamage(AttackInfo)
					if Return == "Hit" or "Kill" then
						local PunchSound = SFX.PunchSound:Clone()
						PunchSound.Parent = Char.HumanoidRootPart
					end
				end

But i dont know if thats the issue or if its the humanoid searching function. I think its the function because ive used some print debugs and found that it put the same humanoid in the list 7 times. And i’ve also checked to make sure that it wasnt the StatModule thing i was using. And it shows that its being trigger 7 times on the same humanoid. Any help’s appreciated!

maybe check to make sure the name of the part is Torso? a lil bit of a sloppy fix but it might work

First, I don’t think there is a blacklist argument being passed into Box. Second, I believe table.find only work if the Humanoids is an ordered array ex: {[1] = “A”, [2] = “B”}. However you are using the part’s order in Instances as the index, so it probably looked like {[1] = “A”, [9] = “B”}, in this case, table.find will work properly for “A”, but return nil for “B”. Making the not table.find(Humanoids, Humanoid) condition true even if it’s already in the table.

Change Humanoids[i] = Humanoid to table.insert(Humanoids, Humanoid) should fix the problem.

1 Like

Try this:

function hitboxModule.GetHumanoids(Instances:{Instance})
	local Humanoids = {}
	local Excluded = {}
		
	for i, instance in pairs(Instances) do
		local Humanoid = instance.Parent:FindFirstChild("Humanoid")
		
		if Humanoid and not table.find(Excluded, instance.Parent) then
			table.insert(Excluded, instance.Parent)
			table.insert(Humanoids, Humanoid)
		end
	end
	
	table.clear(Excluded)
	
	return Humanoids
end

So i tried this, and i also added the blacklist. And it worked! But now the blacklist thing is giving me the error “Unable to cast value to object” Any idea what it is? The instance i put into the blacklist was just the players character to prevent them from hitting themselves.

Edit: I fixed it. I was inputing the player character but i needed to put it in a table to input it. Thanks for the help!

I suggest using debounce to prevent from being hit several times at the same frame.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.