How to make melee hitbox

Hey I wanted to make a melee skill, where someone presses a button and the thing in front of them gets damaged.

So I got the button press working, its just the htibox I’m having trouble with. I’m using a run service stepped function to place the hitbox’s position in front of the player server sided, however its kind of delayed. And that’s really bothering me. Is this the correct way to make a melee attack or is there a better way to this?

Create the hitbox on the client. If the client detects it hit something, it tells the server what it claims to have hit. Then the server does a sanity check. Could be as simple as a magnitude test between the player and it’s target. This isn’t 1,000% exploit-proof but for most cases it’s good enough.

On the client that says it hit something, it should assume it’s hit is valid. Play the animation, sound, etc. Rarely will the hit not actually be valid unless the player is exploiting.

1 Like

Thanks, this is super helpful! I’ll probably use your method, but I was wondering about using shape casts on server side, will this produce the same delayed result?

Not sure what you mean by shape cast, but for something like melee combat where you want the result to feel instant and satisfying, any time you have to wait on the server to give you some information back, it’s going to feel slow. You have to anticipate the hit being accurate and proceed accordingly.

This should suit your needs.

1 Like

Would this still give a delayed client - server effect? seems like this module is for people who want to make complex hitbox shapes

I feel like updating a hitbox every frame, for every player, is gonna cause some lag. You might want to use workspace:GetPartBoundsInBox() instead.

I don’t think it will cause that much performance issues, the reason I updated the hitbox every frame is because the player moves really fast, and placing a static hitbox wouldn’t be accurate

But wouldn’t updating the hitbox every frame give the same result as a static hitbox?

Placing a static hitbox/getboundsinbox() when they use the skill, would yield the exact same amount of accuracy, if not more.

In my game I use

local function addHB(Ch:Model)
	--------------------------------------------------------
	if Ch:FindFirstChild("HitBox") then  -- Don't run the script again
		return 
	end
	--------------------------------------------------------
	local ChHRP		= Ch.PrimaryPart
	local cf,size	= Ch:GetBoundingBox()
	local HitBox 	= HitBox0:Clone()
	HitBox.Size		= size 
	HitBox.CFrame	= cf
	local d			= cf:Inverse() * ChHRP.CFrame
--	local WC		= HitBox:FindFirstChild("WeldConstraint")
--	WC.Part1 		= ChHRP
	local Weld		= Instance.new("Weld")
	Weld.Part1		= ChHRP  
	Weld.Part0		= HitBox
	Weld.C0			= d 
	Weld.C1			= CFrame.identity
	Weld.Parent		= HitBox
	Weld.Enabled		= true 
	HitBox.Parent	= Ch
end


image

works perfectly. I also set the CanTouch of all other baseparts in the Character to false.

1 Like

Not really, instead of making hitboxes for a character you can make hitboxes for melees using modules if you use attachments.

oh yeah thanks! still a little bit of delay but now I don’t have to update hitbox position with runservice. Ran into a little problem where player momentum is stopped everytime I welded a part, but setting massless to true fixed that

if the player activated the melee ability in the air to drop down on the enemy, the static hitbox would be placed at that very moment. meaning the hitbox would never hit the enemy that was below the player

Is the hitbox detecting when you get hit or when your weapon hits them?

the hitbox is for when the weapon hits them

Oh, ok. Then my answer is valid. I saw other people giving you answers for the opposite so I was concerned.

1 Like