How should I stop a hitbox from damaging the person who spawned it?
local HitboxHandler = {}
local hitboxEvent = game.ReplicatedStorage:WaitForChild("HitboxSpawn")
function HitboxHandler.CreateHitbox(Shape: Enum.PartType, Parent, HumanoidRootPart, X, Y, Z)
newHitbox = script.Parent:WaitForChild("Hitbox"):Clone()
newHitbox.Shape = Shape
newHitbox.Size = Vector3.new(X, Y, Z)
newHitbox.CFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -5)
newHitbox.Parent = Parent
end
function HitboxHandler.Damage(Damage)
for _, v in workspace:GetPartsInPart(newHitbox) do
if v.Parent:FindFirstChild("Humanoid") then
v.Parent.Humanoid:TakeDamage(Damage)
break
end
end
end
return HitboxHandler
You can get the character from it’s humanoidRootPart here using humanoidRootPart.Parent
Do this for the damage function:
function HitboxHandler.Damage(Damage, character) -- send the character here
for _, v in workspace:GetPartsInPart(newHitbox) do
if v.Parent:FindFirstChild("Humanoid") and character ~= v.Parent then
v.Parent.Humanoid:TakeDamage(Damage)
break
end
end
end
I was going to reply this, but I’ll also suggest to use GetPartBoundInBox over GetPartsInPart as the latter can be very slow, especially with many unions and meshparts. It also accepts an OverlapParams.
I’m pretty sure it’d be like this:
local HitboxCheckParams = OverlapParams.new()
workspace:GetPartBoundsInBox(newHitbox.CFrame, newHitbox.Size, HitboxCheckParams)
GetPartsInPart precisely checks for overlaps using the physics engine, which can take a bit of time to calculate, as it needs to consider every detail if there is a mesh. GetPartBoundsInBox just checks the bounding boxes without using the physics engine, which is much faster as it skips over the detail. For example, if I had this tree:
GetPartsInPart would check if something is in each detail, so every leaf, etc.
GetPartBoundsInBox, meanwhile, would just check if somethings inside this selection box.
Ah I see, so this shouldn’t be used too much when dealing with models right? In the case of player models, would this be the ideal method when dealing with hitboxes?