Help with hit detection

Hello, I’m currently trying to figure out the best way to detect when a player touches a part, but I’m running into some issues.

What I have tried so far are Touched events and GetPartsInPart, but both have their own problems. Firstly, Touched events can be easily bypassed if an exploiter deletes the touch interest inside the part. So, I tried using GetPartsInPart on the server side, but if I had a lot of kill bricks, checking each one every Heartbeat would probably drain server performance.

I’m just asking if there is another method to detect hits. Thank you!

1 Like

Use GetPartsInPart on the humanoidrootpart of each character, which does the exact same thing but scales with players instead of killbricks

Or maybe gettouchingparts is better
https://create.roblox.com/docs/reference/engine/classes/BasePart#GetTouchingParts

1 Like

well, gettouchingparts depends on physics contac and sometimes cant work with “CanColide = false”. So its better to use GetPartsInPart(HumanoidRootPart)

1 Like

But wouldn’t this only detect hits for the HumanoidRootPart, or do you mean checking every body part of a character with GetPartsInPart?

1 Like

The best way is probably just checking every single character’s parts every heartbeat. It really depends on your game though, if there’s expected to be more character parts than there are killbricks, you should loop through all of the killbricks. If there’s more killbricks than there are character parts, then you should loop through all of the character parts.

Just use RunService.Heartbeat on whichever you prefer, if there’s a massive amount of character parts, you could just loop through all of the killparts on each Heartbeat, like this for example:

local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")

local KillParts = CollectionService:GetTagged("KillParts")

local debounce = 0.1
local alpha = 0

RunService.Heartbeat:Connect(function(dt: number)
	alpha += dt
	if alpha < debounce then
		return
	end
	alpha = 0
	for _, Brick in KillParts do
		if Brick:IsA("BasePart") then
			local PartsInBox = workspace:GetPartBoundsInBox(Brick.CFrame, Brick.Size)
			local HumanoidsDamaged = {}
			
			for _, Part in PartsInBox do
				local Model = Part:FindFirstAncestorOfClass("Model")
				if Model and Model:FindFirstChildWhichIsA("Humanoid") then
					local Humanoid = Model:FindFirstChildWhichIsA("Humanoid")
					
					if table.find(HumanoidsDamaged, Humanoid) then
						continue
					end
					table.insert(HumanoidsDamaged, Humanoid)
					
					Humanoid:TakeDamage(20)
				end
			end
		end
	end
end)

I also added a little debounce to it :grin:

1 Like