Hitbox Scripting

Type of Script: Hitbox
Goal of Script: create a hitbox out of two positions (based on model/part size) and detect if another player’s character is within the hitbox

(not added yet)
my plan for hitbox detection is to loop through all of the player’s characters and check if they are within the hitbox (I would check based on model size and model position)

Code
local Box = workspace:WaitForChild("Box")

local HitBox = Instance.new("Part") -- hitbox marker to show the hitbox in 3D view, hitbox is just going to be two positions without parts when implemented
HitBox.Anchored = true
HitBox.Name = "HitBox"
HitBox.BrickColor = BrickColor.new("Bright red")
HitBox.Size = Vector3.new(.2, 1, .2)

function GenHitBox()
	
	local Size = Box.Size
	local BoxCFrame = Box.CFrame
	local BoxPos = Box.Position
	
	local SizeX = Size.X
	local SizeY = Size.Y
	local SizeZ = Size.Z
	
	local LookVector = BoxCFrame.LookVector
	local RightVector = BoxCFrame.RightVector 
	local Hit = HitBox:Clone() -- first hitbox marker
	Hit.Position = BoxPos + ((RightVector * -1) * (SizeX * 2)) + (LookVector * (SizeZ / 2))
	Hit.Parent = workspace
	
	local Hit2 = HitBox:Clone() -- second hitbox marker
	Hit2.Position = BoxPos + (RightVector * (SizeX * 2)) + (LookVector * (SizeZ * 3))
	Hit2.Parent = workspace
	
end

GenHitBox()

I feel like I may be doing more math then necessary, but I’m not sure

Also is my idea for detection efficient?

image Hitbox in game

3 Likes

Touched, Region3 and Rotated Region3 can do the same thing, go for Touched it’s more performance friendly that’s only if you are going to use CFrame Maths if not go for Region3 or Rotated Region3.

2 Likes

Correct me if I am wrong, but how I do my hit boxes is I do a part and .Touched event. It works perfectly fine and takes less work then doing that type of hitbox.

hitbox = script.parent --the part that is the hitbox

hitbox.Touched:Connect(function(Obj) --The variable you put into these parentheses will automaticly be assigned an object value. This object value is the Object that object that touched the hitbox. 
       --Code here
end)  
2 Likes

I just ended up using regions, in particular rotatedregion3 which @DEVaGame suggested it works pretty well part hitboxes would probably be more efficient though but rotatedregion3 was more suited for what I needed to do

my original idea wasn’t very good compared to other options

1 Like

Region3 is the most precise way of detecting if a part has entered some certain space or to simplify, it’s the most precise for hit detection. .Touched doesn’t work with Body Mover / CFrame Physics, this means if you CFrame a Part moving .Touched will not pick that up, that is why I always use Region3 for my Beams / Projectiles in my Dragon Ball projects. RotatedRegion3’s concept works like a normal Region3 however it simulates a rotated version.

1 Like

Touched + CFrame is only good on the client as I recall, it’s iffy on the server though