Hitbox Script Feedback

i made this hitbox just now and what do you think?
is this a good hitbox?
i made a alternative to a weld to avoid lag.
do you think this is a good hitbox module?
and yes i did use some chat gpt on this…

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local HitboxSpawner = {}
HitboxSpawner.__index = HitboxSpawner

function HitboxSpawner.new()
	local self = setmetatable({}, HitboxSpawner)
	self.Size = Vector3.new(6, 6, 6)
	self.Offset = CFrame.new(0, 0, 0)
	self.onTouch = function() end
	self._running = false
	self._rootPart = nil
	self._detectedHumanoids = {}
	self._spawnInterval = 0.05
	self._thread = nil
	return self
end

function HitboxSpawner:_spawnHitbox()
	if not self._rootPart then return end

	local part = Instance.new("Part")
	part.Size = self.Size
	part.Transparency = 1
	part.Anchored = true
	part.CanCollide = false
	part.CFrame = self._rootPart.CFrame * self.Offset
	part.Name = "TempHitbox"
	part.Parent = workspace

	local box = Instance.new("BoxHandleAdornment")
	box.Adornee = part
	box.Size = self.Size
	box.Color3 = Color3.new(1, 0, 0)
	box.Transparency = 0.75
	box.AlwaysOnTop = false
	box.ZIndex = 0
	box.Parent = workspace

	-- Detect humanoids in the area
	local partsInBox = workspace:GetPartBoundsInBox(part.CFrame, self.Size)
	for _, part in ipairs(partsInBox) do
		local model = part:FindFirstAncestorOfClass("Model")
		local humanoid = model and model:FindFirstChildOfClass("Humanoid")
		if humanoid and not self._detectedHumanoids[humanoid] then
			self._detectedHumanoids[humanoid] = true
			self.onTouch(humanoid)
		end
	end

	-- Cleanup
	Debris:AddItem(part, 0.1)
	Debris:AddItem(box, 0.1)
end

function HitboxSpawner:Start(rootPart, offset)
	if not rootPart then
		warn("No rootPart provided to hitbox.")
		return
	end

	self._rootPart = rootPart
	if offset then
		self.Offset = offset
	end
	self._running = true
	self._detectedHumanoids = {}

	-- Spawn immediately
	self:_spawnHitbox()

	-- Then continue every 0.05s
	self._thread = task.spawn(function()
		while self._running do
			task.wait(self._spawnInterval)
			self:_spawnHitbox()
		end
	end)
end

function HitboxSpawner:Stop()
	self._running = false
	if self._thread then
		task.cancel(self._thread)
		self._thread = nil
	end
end

return HitboxSpawner

i need some feedback as soon as possible to choose if its the right choice for my game!

3 Likes

Why don’t you just use blockcast/shapecasts? They might be more efficient than this.

1 Like

Hmm i didnt know that existed. But anyways im gonna try. Ty