Lag with BasePart.Touched

So I’m making a big enemy in my game that has 3 HitBoxes. 1 For him taking damage, and 2 for him dealing damage to enemy’s. Once the Big enemy uses an attack the BasePart.Touched gets activated and records EVERYTHING that it touched and its always active which creates a lot of lag, + every time the big enemy’s hitbox touches the players hitbox, more and more lag gets generated overtime and the lag reaches 40% script activity

the code responsible for the damage:


				script.Parent.Hammer.Blade.Touched:Connect(function(hit)
						
					if AttackTime and hit.Parent:FindFirstChild("Humanoid") and not CollectionService:HasTag(hit.Parent.Humanoid, "Damaged") then
						
						if game.Players:GetPlayerFromCharacter(hit.Parent) then
							
							local player = game.Players:GetPlayerFromCharacter(hit.Parent)
							
							local CanBeDamaged = hit.Parent.RemoteHit:InvokeClient(player)
							
							if CanBeDamaged == "true" and not CollectionService:HasTag(hit.Parent.Humanoid, "Damaged") then
								
								local Damage = require(game.Workspace.Scripts.DamageScript)
								local Enemy = hit.Parent.Humanoid
									
								CollectionService:AddTag(Enemy, "Damaged")
									
								Damage.DoDamage(Enemy, 50)
							
							end
							
						end
							
					end
						
				end)

INTRO

It’s not a good idea to use .Touched for damaging stuff, it relys on physics and most of the time it’ll be inaccurate. If this big enemy we’re talking about swings a sword, it might be best to install a Raycast Hitbox Module, which is here: Raycast Hitbox 4.01: For all your melee needs!

OTHER SOLUTIONS

You could use Raytracing to detect players getting hit by Big Enemy. The link I provided has a code example, so you should be good.

GENERAL CODE REVIEW

if game.Players:GetPlayerFromCharacter(hit.Parent) then

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
							
local CanBeDamaged = hit.Parent.RemoteHit:InvokeClient(player)
							

This part has quite a bit of flaws. Firstly, the if game.Players:GetPlayerFromCharacter(hit.Parent) should be saved into a variable, to prevent further yielding and visible “hops.” Also, when you call :InvokeClient on a RemoteFunction The scripts yields waiting for that response, so essentially, a exploiter could override the Invoke callback on the client and break your entire script.

The part where when we can apply damage to the player also has a flaw, you should really save the Damage module in a local reference, rather than requiring it every time, which would be extremely expensive (I think.) when using physics-based events.

IN ALL,

Don’t use .Touched for hit detection, it’ll save you tons of headaches and testing.

got it
tnx… :+1: :+1: :+1: :+1: :+1: :+1:

1 Like

FastCast is for ranged weapons. If you want sword hitboxes, consider using a raycast method instead. If you look through Community Resources, someone has made a hitbox for melee weapons:

1 Like

Oops, I totally got them both mixed up. Thanks.