Hitbox issue it works but its not hitting multiple targets

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    fix hitbox
  2. What is the issue? Include screenshots / videos if possible!
    it currently hits one target and it hits weirdly if its 2 target like itl hit through one target to get to the other
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    idk asking for help
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local HitboxModule = {}
local Fx = workspace.Fx

local isFront 

function HitboxModule.NormalHitbox(size, cframe, ignore, char)
	local hitbox = Instance.new("Part", Fx)
	hitbox.Size = size
	hitbox.CFrame = cframe
	hitbox.Transparency = 0.5
	hitbox.Material = Enum.Material.Neon
	hitbox.Color = Color3.new(1, 0, 0)
	hitbox.CanCollide = false
	hitbox.CanTouch = true 
	hitbox.Massless = true 
	hitbox.Anchored = false  --reverse if hitbox issue return 
	hitbox.CastShadow = false

	hitbox.Name = "HitBox"
	
	--remove if hitbox issue return 
	
	--local weld = Instance.new("WeldConstraint", hitbox)
	--weld.Part0 = char.HumanoidRootPart
	--weld.Part1 = hitbox

	local hitTarget = nil
	local ignoreSet = {}
	for _, v in pairs(ignore) do
		ignoreSet[v] = true
	end


	local function onHit(part)
		local parent = part.Parent
		if parent:FindFirstChild("Humanoid") and not ignoreSet[parent] then
			if part.Name == "HumanoidRootPart" then
				local dot = char.HumanoidRootPart.CFrame.LookVector:Dot(part.CFrame.LookVector)
				if(dot >= -1 and dot < 0) then
					isFront = true
				else
					isFront = false
				end
			end
			hitTarget = parent
		end
	end

	--task.wait() 
	local partsinsideofhitbox = workspace:GetPartsInPart(hitbox)
	for _, v in pairs(partsinsideofhitbox) do 
		onHit(v)
	end
	print(hitTarget)

	 
	--weld:Destroy()
	hitbox:Destroy()
	return hitTarget, isFront
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

There is a couple of things I see that may cause issues. If you plan to leave your weld code commented you should set anchored to true. You will only return one target with local hitTarget = nil. You set hitTarget = parent which allows a target to be overwritten by another. Your code stores your facing check isFront as a single variable, so if multiple targets were hit, you’d lose the individual result for each.

hitTarget is one variable, not a table. And because workspace:GetPartsInPart() gets all of the parts while respecting the ‘params’, if there were multiple targets in front of you, then they would all get overridden.

Changing hitTarget into an empty table and using table methods such as table.insert and table.find would be the way to do it.

I decided to share with you the modified script.

local Fx = workspace.Fx

local isFront 

function HitboxModule.NormalHitbox(size, cframe, ignore, char)
	local hitbox = Instance.new("Part", Fx)
	hitbox.Size = size
	hitbox.CFrame = cframe
	hitbox.Transparency = 0.5
	hitbox.Material = Enum.Material.Neon
	hitbox.Color = Color3.new(1, 0, 0)
	hitbox.CanCollide = false
	hitbox.CanTouch = true 
	hitbox.Massless = true 
	hitbox.Anchored = false  --reverse if hitbox issue return 
	hitbox.CastShadow = false

	hitbox.Name = "HitBox"
	
	--remove if hitbox issue return 
	
	--local weld = Instance.new("WeldConstraint", hitbox)
	--weld.Part0 = char.HumanoidRootPart
	--weld.Part1 = hitbox

	local hitTarget = {}
	local ignoreSet = {}
	for _, v in pairs(ignore) do
		ignoreSet[v] = true
	end


	local function onHit(part)
		local parent = part.Parent
		if parent:FindFirstChild("Humanoid") and not ignoreSet[parent] then
			if part.Name == "HumanoidRootPart" then
				local dot = char.HumanoidRootPart.CFrame.LookVector:Dot(part.CFrame.LookVector)
				if(dot >= -1 and dot < 0) then
					isFront = true
				else
					isFront = false
				end
			end
			if not table.find(hitTarget, parent) then
				 table.insert(hitTarget, parent)
			end
		end
	end

	--task.wait() 
	local partsinsideofhitbox = workspace:GetPartsInPart(hitbox)
	for _, v in pairs(partsinsideofhitbox) do 
		onHit(v)
	end
	print(hitTarget)

	 
	--weld:Destroy()
	hitbox:Destroy()
	return hitTarget, isFront
end

This is the best solution I could do as I couldn’t find an efficient way to add the inFront Boolean variable.

using a mobile device

i tried usign that script but hitbox stopped working