Best method to create a hitbox welded to a moving character?

Hey there. I know there are tons of topics like this, but I’ve been searching for a while and couldn’t find any useful for my problem.
So I create a body velocity inside the HumanoidRootPart of my character, which I might change to tweening it. Anyways, this moves the character forward, while performing an attack. I create a hitbox with collision set to false, cframed in front of the character and weld it with WeldConstraint. The issue is that hitbox doesn’t work properly. It always hits if it’s created in the area, where enemy stands already. But hitbox hits only sometimes in other cases.
I’ve tried to make a simple .Touched event, it’s not really reliable, same with for i,v in pairs(workspace:FindPartsInPart(hitbox)) do, that is connected to a server Heartbeat for 0.2 seconds. After disconnecting the Heartbeat, the hitbox is destroyed too, so early disconnection is not the issue.
I will apply the video when roblox is fixed.
Any feedback is appreciated!

1 Like

Use the Touched event to detect whenever a part is hit by another part, then just determine if the part being hit belongs to the character and the hitting part is some projectile/weapon etc.

https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched

There are issues with using Touched, especially when you have higher velocity Parts (bullets, swords, etc.).
Have you tried Raycasting | Roblox Creator Documentation instead?

2 Likes

How exactly should I use raycast for this? My character flies forward, but rays are so small, they will hit only if the character is looking right at the enemy. At least I understand it so.

Thank you for the feedback, but it doesn’t work for me properly. Maybe you have some tips on how to use it so it would work?

For reliable hit detection please do not depend on .Touched. The latency disconnect between client and server that can occur in-game makes proper hit detection using Touched almost impossible.

To me, a hitbox should be hit, not attempting to gather what hit it. This is why Raycasting, as Scottifly said, is a better method. The reliability of any method that depends on your hitbox accurately describing its bounded parts (or anything of that nature) is going to get increasingly worse the more latency variation that exists.

If you can explain your use-case further, you can receive further assistance. What objects does the Hitbox need to register contact with?

(meant to reply to OP)

i think it will work.

game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local part = game.ReplicatedStorage.Part:Clone()
part.Parent = character

local Weld = Instance.new("Weld", part)
Weld.Name = "Weld"
Weld.Part0 = part
Weld.Part1 = character.Head

end)

do you want to try magnitude too?

Using :FindPartsInPart is probably the best method for your case. Try changing the position or size of the hitbox - also, are you checking 0.2 second after you create the hitbox, or does it check instantly?

Ideally if the attack starts from the client, you should do some effects on the client to get instant feedback, and tell the server which character you’ve damaged. The server can then check and properly damage them, so that you always hit the targets instantly without ping being a factor.

You could use a few rays that are offset to gather information from an area ahead of the player.

I do it after creating the hitbox, since for i,v in pairs(workspace:GetPartsInPart(hitbox)) would make no sense, because the hitbox is not created yet, but I may be wrong.

Do you recommend me to make the hitbox check on the client of the attacking player, after that fire the server for damaging enemies?

I can’t offer the right code I made, since roblox is down at the moment, but I can try to:
So first, there’s an animation with animation events

--Playing anim
Anim:GetMarkerReachedSignal("Dash"):Connect(function)
local BV = Instance.new("BodyVelocity",Humrp)
BV.MaxForce = Vector3.new(1e5,1e5,1e5)
BV.Velocity = Humrp.CFrame.lookVector * 35
Debris:AddItem(BV,.2)
end)

Anim:GetMarkerReachedSignal("Hitbox"):Connect(function)
local hitbox = script.hitbox:Clone()
--CFraming it
local weld = Instance.new("WeldConstraint")
weld.Part0 = Humrp
weld.Part1 = hitbox
weld.Parent = Humrp

local Connect = RunService.Heartbeat:Connect(function)
for i,v in pairs(workspace:GetPartsInPart(hitbox)) do
-- checking if it's a not our character and has a humanoid
--deals damage etc.
end
end)
task.wait(.2)
Connect:Disconnect()
hitbox:Destroy()
weld:Destroy()
end)

That’s the code I’m using for my dash attack. I am not sure, if I wrote what you wanted to hear, but I hope it helps you understand the issue more.

I don’t think magnitude will work, unless you have a suggestion.

By this, I mean are you making the hitbox, waiting 0.2 seconds, and THEN checking, or are you making the hitbox and checking instantly after you make it?

And yes, I would recommend that very much. Your issue might just be ping, since on the client an enemy might appear like they should be hit, but when the signal is sent and then the server checks, it could’ve already moved away.
I’d suggest doing the hitbox on the client, and then doing another slightly larger one on the server to validate it, making sure it’s within a reasonable distance (not too far otherwise it’ll be easily exploited).

Your method sounds right, but I don’t think the issue is ping. I was testing it on dummies that were standing still. As I’ve mentioned, the hitbox always works if it’s created in the area with an enemy already standing there. Else, if you fly towards the enemy with a hitbox, it may not hit the enemy. The issue may be caused by BodyVelocity, maybe I should try tweening the character’s cframe. Anyways, I’ll try your method when roblox is fixed.

Could you apply a little script example of it, please?

and

are modules that are specifically designed for an accurate melee system.

All you have to do is add attachments to the weapon i.e. the hand of the character. Then you can use either of these modules to record what is hit.

I have used both modules and from what I have seen RaycastHitbox is slightly easier to use simply because it detects humanoids. But for most of the time they are identical.

If you need any help with these there are docs on how you can use them.

My raycast hitbox seem to lag behind when my character moves am I doing something wrong?

Doesn’t seem like it. Probably make sure your attachments are in the right place. Also, you may want to make a note that the visualizer isn’t optimized since it has to instantiate parts. It could just be that.

1 Like

most likely its because the raycasts are serversided, it might look like it lags behind in your screen but it will look perfectly fine to others

1 Like