Optimal way to do hit detection

I’ve always heard to do .Touched for hit detection along with a debounce to prevent multiple hits.
However, is there another way (possibly better way) to do this?
For melee combat that is.

1 Like

For melee combat, assuming you just want the damage to be dealt once per hit, keep a table every swing (in the connection itself preferably) and make a key for every victim. Before dealing damage, check if a victim already exists as a key.

Oh yes, I’ve done this before. I was just wondering about alternatives to .Touched like FindPartOnRay.

I use explosions.
Blast pressure of 0, visible = false



function IsInTable(Tab,Item)
	for i,v in pairs(Tab) do
		if v == Item then
			return true
		end
	end
	return false
end


		local expl = Instance.new("Explosion")
		expl.BlastPressure = 0
		expl.Visible = false
		expl.BlastRadius = Properties.Range.Value
		expl.Position= Character.HumanoidRootPart.Position
		expl.DestroyJointRadiusPercent = 0
		expl.Parent =workspace
		
		local HE = expl.Hit:Connect(function(H)
			if not H:IsDescendantOf(Character) then
				if H.Parent:FindFirstChild('Properties') and not IsInTable(Hits,H.Parent) and H.Parent.Properties:FindFirstChild('Attackable') then
					table.insert(Hits,#Hits+1,H.Parent)
					local Vec = -((Character.HumanoidRootPart.Position-H.Parent.HumanoidRootPart.Position)*Vector3.new(1,0,1)).unit
					local LV = (Character.HumanoidRootPart.CFrame.lookVector*Vector3.new(1,0,1)).unit
					local Angle = math.deg(math.acos(Vec:Dot(LV)))
					print(Angle)
					if Angle < Tool.Properties.SwingAngle.Value then
						--do damage
					end
				end
			end
		end)
10 Likes

Wouldn’t that cause extra lag due to the fact there’s extra math involved?

3 Likes

That’s amazing

local expl = Instance.new("Explosion")
expl.BlastPressure = 0
expl.Visible = false
expl.BlastRadius = Properties.Range.Value
expl.Position= Character.HumanoidRootPart.Position
expl.DestroyJointRadiusPercent = 0
expl.Parent = workspace

local Vector3101 = Vector3.new(1,0,1)

local Hits = {}

local function Hit(H)
	if not H:IsDescendantOf(Character) then
		if H.Parent:FindFirstChild('Properties') and not Hits[H.Parent] and H.Parent.Properties:FindFirstChild('Attackable') then
			Hits[H.Parent] = true
			local Vec = -((Character.HumanoidRootPart.Position - H.Parent.HumanoidRootPart.Position) * Vector3101).unit
			local LV = (Character.HumanoidRootPart.CFrame.lookVector * Vector3101).unit
			local Angle = math.deg(math.acos(Vec:Dot(LV)))
			print(Angle)
			if Angle < Tool.Properties.SwingAngle.Value then
				--do damage
			end
		end
	end	
end

local HE = expl.Hit:Connect(Hit)

I removed the For Loop because it’s not needed anymore

if not Hits[H.Parent] then end -- I'm checking if H.Parent is inside Hits as a Key


Hits[H.Parent] = true -- I'm Setting H.Parent as a Key and True as a Value inside Hits

table.insert(Table,Value), you don’t need to include the Pos if it’s going to the End of the Table

local Vector3101 = Vector3.new(1,0,1) – because you are using the same Value making it a Variable is better

Consider this

local Print = print

Print('Hello')

@xuefei123 Anything you do would cause the Computer to work more anyways I think in this case it’s not a lot of math I don’t think it will effect the performance.

4 Likes

It would also be especially efficient on area effects without the math.
I’ll keep that in mind.

You might like using Meta tables & Meta methods

I find them very interesting to use and very powerful

I am definitely going to use it for my Newer Projects because my current project has about 4-5K lines of code and man I already changed it once for Performance but I don’t wanna do that again maybe later after the game is Released and is FP.

Learn More About OOP

1 Like