Hitbox reviewal

i’m making a fighter and, i used a hitbox system my friend worked on and i tweaked it heavily. I was unpleased with the performance after doing ~20 attacks, so i was wondering what you guys suggest adding / changing to make this hitbox system more performant.


CODE -

-- services --
local players = game:GetService("Players")
local runservice = game:GetService("RunService")

-- objects --
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local remotes = game:GetService("ReplicatedStorage").remotes
local hitbox = remotes.Hitbox

-- variables --
local connection

-- modules --
local region_module = require(game.ReplicatedStorage.modules.REGION_MODULE)
local hitbox_module = require(game.ReplicatedStorage.modules.HITBOX_MODULE)

-- events --
connection = hitbox.OnClientEvent:Connect(function(region,duration,cframe,dmg)
	local stopped_attack = false
	repeat runservice.Stepped:Wait()
		local target_hit = false
		local enemy_hum
		if not enemy_hum then
			for i,v in ipairs(region_module:CastRegion(character,1,region)) do
				if v.Parent and v.Parent:FindFirstChildOfClass("Humanoid") and (v.Parent:FindFirstChild("Torso")) then
					enemy_hum = v.Parent:FindFirstChildOfClass("Humanoid")
				end
			end
		end
		if enemy_hum and enemy_hum.Health > 0 and enemy_hum.Parent:FindFirstChildOfClass('ForceField') == nil and not players:GetPlayerFromCharacter(enemy_hum.Parent) then
			local results = hitbox_module:HITBOX(nil,cframe,dmg,enemy_hum,player,duration)
			if results then 
				connection:Disconnect()
				target_hit = true
				--[[local bvelo = Instance.new("BodyVelocity")
				bvelo.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
				bvelo.Velocity = CFrame.new(enemy_hum.Parent.Torso.Position - (enemy_hum.Parent.Torso.Position - character.HumanoidRootPart.Position).unit*3,enemy_hum.Parent.Torso.Position).lookVector*25
				bvelo.Parent = enemy_hum.Parent.Torso
				Debris:AddItem(bvelo,.2)]]--
			end
			return results
		end
	until stopped_attack == true or target_hit == true
	spawn(function()
		wait(duration)
		connection:Disconnect()
		stopped_attack = true
	end)
end)

I really am not quite familiarized with hitboxes, raycasting etc… But i’ve came across some threads that could really help a ton. I wish you the best of luck !

Have you done your own benchmarking to try and narrow down where your performance hits are coming from? It would help you form a better Code Review because you could ask for optimisation tips specifically on the parts that are causing your slowdowns rather than throwing the entire code onto a thread and asking if anyone can look at the whole thing.

Consider use of the MicroProfiler and the debug library, or simple print statements with os.clock.

It seems after benchmarking the events may not be disconnecting after the time of the attack is up.

The issue is performance, not hitbox detection. I already have the hitbox system use Region3