# What Is The Most Efficient Way To Deal Damage To Multiple Players With Melee Weapons?

I am currently programming a game which requires a lot of melee weapons to be scripted. I was wondering what would be the most efficient way to deal damage to multiple players/NPCs at once. I have seen multiple methods being used such as using Region3, but I am not sure how I would go about doing this.

Could you make your question more specific? Are you looking into how to just do damage to a player? or check the hitbox for a weapon? if you are doing hitbox, are there any specifics to it (ie; geometry, special cases, idk)

If you are trying to simply deal damage to a humanoid, you can use humanoid:TakeDamage(amount)

A partâ€™s `.Touched` event would probably be the most efficient, but you shouldnâ€™t base which method to use solely on efficiency.

I know how to damage a humanoid. I am not that dense. I am asking on how I can damage multiple humanoids efficiently.

By â€śefficientâ€ť I mean a simple & good method.

If you have multiple humanoids in a table, you can use a `for` loop.

As an example, this code snippet would deal 10 damage to 3 humanoids in an array named `humanoids`.

``````local humanoids = {hum1, hum2, hum3}
for i,v in pairs(humanoids) do
v:TakeDamage(10)
end
``````

Alright but how would I go about inserting multiple humanoids in the â€śhumanoidsâ€ť table? That is my question. More like what is the best way of doing so. `.Touched` event? `Region3`? etcâ€¦

So you are looking for ways to implement a weapon hitbox. Like I asked in my initial response, are there any specifics to your hitbox? Is it a rectangle? Is it a radius around a part? Is it convex/concave polygon, time variant points in space, etc?

In general, most optimization in Software can be done once you know the specifics to your problem.

I did not get what you meant by a weapon hitbox, sorry. Also consider it as an object with an irregular shape as I will have different weapons with different shapes.

Are you looking to deal damage to multiple players at the same time?
If so, I encourage you to read the wiki article on Region3:

https://developer.roblox.com/api-reference/datatype/Region3

As well as looking into workspaceâ€™s Region3 functions which youâ€™ll be using (scroll down to Functions):

https://developer.roblox.com/api-reference/class/Workspace

If youâ€™re looking to damage players only when they touch the blade of your weapon, but also keeping the script from damaging the same player several times, I came up with the following idea:

For each time your .Touched event fires, you have to check if what it returned contains a Humanoid.
Once you did that, you can add this humanoid to a table inside of your script (or a ModuleScript).
Using this table, you can check whether or not the table contains the humanoid youâ€™ve just hit.
Table doesnâ€™t contain humanoid? Add it to the table and deal damage
Table contains humanoid? Return

2 Likes

OK so the majority of the ideas here are not efficient. .Touched is definitely not efficient.

I would recommend using a Region3. Youâ€™d have to simplify the HitBox, but all major games do the, and it is unnoticeable, I assure you, especially if the melee weapon is moving at speed.

Every frame or tick, on the server just redefine the Region3. Then, search the Region3 for BaseParts within characters, using workspace:FindPartsInRegion3WithWhiteList(). Now, just loop through this table and for each BasePart, find which character it corresponds to, and insert that character into a table of characters to damage. Now just loop over that table of characters and deal damage to them.

2 Likes

Alright then, so Region3 it is. Thanks!

1 Like