Combat doesn't detect hit sometimes?

Hello.

While I was testing combat with a few people, some times my combat doesn’t detect hit some times. It works well for the most but like I said it doesn’t detect hits some times from certain angles.

Here is the code that does the detection:

for i,v in pairs(game.Workspace:GetChildren()) do
	if v:FindFirstChild("Humanoid") and v:FindFirstChild("HitBox") then
		local HitBox = v:FindFirstChild("HitBox")
		local Human = v:FindFirstChild("Humanoid")
		if HitBox and Human and HitBox ~= Character:FindFirstChild("HitBox") then
			if (HumanoidRoot.Position - HitBox.Position).magnitude <= Range then
			local detecting = Ray.new(HumanoidRoot.Position, HumanoidRoot.CFrame.LookVector.Unit * Range)
			local hit,position = game.Workspace:FindPartOnRayWithIgnoreList(detecting, {HumanoidRoot})
			if hit then
				--code here
			end
		end
	end
end
end

I welded a part to every player in the game called “HitBox”, then when you click it loops through workspace to find anyone near you using magnitude then if they are within the magnitude it uses raycast to make sure they are in front of you.

1 Like

Wouldn’t Touched events do a better job at this ?

1 Like

I’ve heard .Touched is inconsistent.

Proof: Touching inconsistency

2 Likes

In addition to this, please don’t loop through every child of the workspace, it’s horribly inefficient.
I believe you’re raycasting through lookvector and not unit.

Theres a nice module for raycast hit detection here

2 Likes

All the weapons I scripted use Touched events and they work extremely good, and if you disconnect the event after running the code, and make sure no memory leaks happen, then they work perfectly fine

1 Like

Do not use touched events for hit detection. Exploiters could register illegal hits by resizing hitboxes if they have network ownership. A great example is the default linked-sword.

3 Likes

The touched event is fired from the client, which means even though you called the function in a server script, the hitboxes is still detected in the client.

Which means exploiters can extend parts to send you to brazil 20000 studs away from you

1 Like