self.Part = hitboxPart self.Damage = damage or 10 self.Owner = owner self.Active = false self.HitCharacters = {} -- track already hit characters
private void OnTriggerEnter(Collider other) { if (!isActive) return; hitbox script
function HitboxModule:Activate() self.Active = true self.HitCharacters = {} self.Connection = self.Part.Touched:Connect(function(hit) if not self.Active then return end public GameObject owner
function HitboxModule:OnHit(character, hitPart) -- override for custom logic (sound, vfx, etc.) print( Hit {character.Name} for {self.Damage} damage ) end private bool isActive = false
public void Deactivate() { isActive = false; }
local humanoid = hit.Parent:FindFirstChild("Humanoid") local character = hit.Parent if not humanoid then -- check if hit is a limb inside a character local limb = hit if limb.Parent and limb.Parent:FindFirstChild("Humanoid") then humanoid = limb.Parent.Humanoid character = limb.Parent end end if humanoid and character ~= self.Owner then if not self.HitCharacters[character] then self.HitCharacters[character] = true humanoid:TakeDamage(self.Damage) -- optional: trigger hit effect self:OnHit(character, hit) end end end) end
-- when swinging hitbox:Activate() task.wait(0.3) -- swing duration hitbox:Deactivate() public class Hitbox : MonoBehaviour { public float damage = 10f; public GameObject owner; private bool isActive = false; private HashSet<GameObject> hitTargets = new HashSet<GameObject>(); public void Activate() { isActive = true; hitTargets.Clear(); }