This is the first module I’ve done in my life so I’m looking for feedback to optimize it as much as possible. Expect errors
The script basically creates the hitbox with the given specs, welds it to a part, and then detects hits using :GetPartsInPart inside a for loop 10 times returning the parent of the humanoid detected.
The script also names the hitbox with your character’s name so you can detect it later.
Currently I would like to find another way to make the loop so that I can detect several humanoids at the same time
local Combat_Handler = {}
function Combat_Handler.Hitbox(Character,Size,Position,Weld,Duration)
local Debounce = {}
local hitbox = Instance.new("Part", workspace)
hitbox.Name = Character.Name.." hitbox"
hitbox.CanCollide = false
hitbox.CanQuery = false
hitbox.Massless = true
hitbox.BrickColor = BrickColor.new("Really red")
hitbox.Transparency = 0.5
hitbox.Size = Size
hitbox.CFrame = Position
local weld = Instance.new("WeldConstraint")
weld.Part0 = Weld
weld.Part1 = hitbox
weld.Parent = weld.Part0
game.Debris:AddItem(hitbox,Duration)
local OP = OverlapParams.new()
OP.FilterType = Enum.RaycastFilterType.Blacklist
OP.FilterDescendantsInstances = {Character}
for i=10, 0, -1 do
for i,v in pairs(workspace:GetPartsInPart(hitbox,OP)) do
local humanoid = v.Parent:FindFirstChild("Humanoid")
local enemyHitbox = workspace:FindFirstChild(v.Parent.Name.." hitbox")
if humanoid and v.Name == "Hitbox" then
if not table.find(Debounce, humanoid) then
table.insert(Debounce, humanoid)
game.Debris:AddItem(enemyHitbox,0)
return humanoid.Parent
end
end
end
print(i)
task.wait(Duration/10)
end
end