How should we do hit detection?

When it comes to making weapons, attacks, etc you need to know how to detect if you hit something.
My question is, whats the best way to do hit detection? What do you use? How should we go about it? Thanks!

1 Like
--Put this in a Touchable object
script.Parent.Touched:Connect(function (hit)
if hit.Name == [Name of the Weapon] then
--Your code
end
end)

Where as yes this is a valid way to do this not sure if its the best. .Touched has always been weird and if you hit and object and instantly bounce off of it nothing will happen.

But thanks for the reply :smiley:

1 Like

Lemme correct it:

–Put this in a Touchable object
local Debounce = false
script.Parent.Touched:Connect(function (hit)
if hit.Name == [Name of the Weapon] then
Debounce = true
–Your code
task.wait(YourTime)
Debounce = false
end
end)

else it will get like 30 hits per touch

Adding a debounce wont fix this problem though

i still didnt understand the problem

Ever heard of a kill brick? Well um if you didnt know jumping on them very fast well you wont get killed by them cause .Touched is rather idk slow ig. It just doesnt work when things are moving fast

Disable Cancollide is the solution

Or create an invisible Killblock Cancollide = false and place it above the main kill block if you want the main on collide enabled

But like what about projectiles? Itll still just fly right through the thing but wont trigger. So no matter what you do .Touched is usually to slow for fast things and wont really work. With slower things like melee weapons yes itll probably work, .Touched is also a little scuffed

1 Like

Then use this to the killblock, then ih should work (reply)

1 Like

For punches or a combat system or sword you should probbaly make a hitbox then use getpartsboundsinbox

Itll work for slower things such as a kill brick but in my case:

It’d be to slow

Ah yes! I’ve heard of this before but never learned about it, Is there a doc on this subject?

yeah sorry then i dont have other solution cuz i didnt went with the time and dont use the new methods.
i just recently read that wait() is outdated and you need to use task.wait()

1 Like

Theres a youtube video that explains this well https://www.youtube.com/watch?v=XNO8G80mJL4

Also you can use :gettouchingparts

Wait i think i found a better video this goes over the overlap params and stuff https://www.youtube.com/watch?v=XAO3HF4vFg0

1 Like

Raycasts should work best for these.

1 Like

While Touched can be used for large, slow projectiles that need physical simulation (I’m picturing like Donkey Kong barrels), anything smaller or faster than a softball is best done with raycasts or shape casts, to avoid the bullet-through-paper issue. Roblox physics simulation does 4 substeps per render frame, but this is for stability and it isn’t continuous collision detection, so you have to write your own, by raycasting or shape-casting between positions each frame.