Stop hitbox from damaging the user

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
1 Like

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
2 Likes

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)
2 Likes

Quick question, could you explain how GetPartBoundInBox is faster than GetPartsInParts? Thanks!

1 Like

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:
image
GetPartsInPart would check if something is in each detail, so every leaf, etc.

GetPartBoundsInBox, meanwhile, would just check if somethings inside this selection box.

It’s a matter of balancing and decision. If you need to check very precisely, use GetPartsInPart, but to check very quickly, use GetPartBoundsInBox.

1 Like

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?

1 Like

yes, that’s correct. (the 120 divided by 4 character limit should be abolished from the roblox devforums, it serves no purpose)

2 Likes

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