HitboxClass | v1.1B | A Powerful OOP-based Hitbox Module

I got some stuff, but, how do I make the blacklist work? It doesn’t do anything, I still get hurt. I haven’t scripted in a long time (only when I have to), so I have no clue how to fix this.

ReplicatedStorage.Events.HitboxClassRemote.OnServerEvent:Connect(function(Player, Blacklist)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild('Humanoid')


	local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart')
	
	local Params = {
		SizeOrPart = 4,
		DebounceTime = 3,
		Debug = true,
		Blacklist = Character 
	}	:: HitboxTypes.HitboxParams
	newHitbox:Start(); newHitbox:WeldTo(HumanoidRootPart, CFrame.new(0, 0, -4))


	newHitbox.HitSomeone:Connect(function(hitChars)
		for _, hit: Humanoid in hitChars do
			if hit:FindFirstChild('Humanoid') then
				hit.Humanoid:TakeDamage(25)				
			end			
		end

	end)
end)

Pass it a table of characters. So just wrap it in braces.

local Params = {
		SizeOrPart = 4,
		DebounceTime = 3,
		Debug = true,
		Blacklist = {Character}
} :: HitboxTypes.HitboxParams

Thanks, now I can finally start working on my randomizer game!!

1 Like

Alright thanks for the help know I can use this properly

how could i make the hitbox follow a projectile or detect if the projectile touches something?

the red hitbox still shows when I turned debug off, how do you fix that?

I have 1 more question, does the debounce timer control how often the hitbox can be triggered globally, or individually?

Can you make an object mode where it can detect both humanoids and objects

Is anyone still in this forum?

Why you need that?
If you need to check both. Use HitObject as Touched.

If hit.Parent.Humanoid ~= nil then

end

I recommend to read the github documentation. It tells about methods and values that methods needs. I did not used full potential yet but module works.
Use alive folder, its faster and more organised.
For player to be in Alive folder use:

game.Players.PlayerAdded:Connect(function(plr)
       plr.CharacterAdded:Connect(function(char)
             char.Parent = workspace.Alive
       end)
end)

It’s not hard.
Btw, the Velocity Prediction is very nice

When I start the Hitbox and then destroy it, I get the following error:
“ReplicatedStorage.Modules.HitboxClass:281: Attempt to compare number <nil”

In this line of the module:

function Hitbox:Start()

	if self.Lifetime > 0 then ---- The error occurs on this line
		
		if not self.Timer then
			
			self.Timer = Timer.new(0.1, function()
				self.Lifetime -= 0.1
				if self.Lifetime <= 0 then
					self:Destroy()
				end
			end)
			
		else
			self.Timer:On()
		end
		
	end

I have tried using :Stop, to stop the Hitbox, that helped the error stop appearing, but it causes several hitboxes to be created and welded to the player. When I use :Destroy it helped me solve that problem, however, I get this error that causes delays in the game.

I also want to show the code, in which I use the module.:

local tool = script.Parent
local remoteEvent = tool.InputAbility

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HitboxClass = require(ReplicatedStorage.Modules.HitboxClass)
local HitboxTypes = require(ReplicatedStorage.Modules.HitboxClass.Types)

local PunchDebounce = false
local Equipped = false

local Player = tool.Parent.Parent
local Char = Player.Character or tool.Parent

local PunchHitboxParams = {
	SizeOrPart = Vector3.new(2.5,2,3.5),
	SpatialOption = "InBox",
	Blacklist = {Char},
	LookingFor = "Humanoid",
	Debug = true,
} :: HitboxTypes.HitboxParams

----------------ANIMATIONS--------------------------

local PunchAnimation = Char.Humanoid.Animator:LoadAnimation(script.Punch)

-------------------------------------------------------------------

remoteEvent.OnServerEvent:Connect(function(plr, Input, Info)
	if Equipped == true then
		local character = tool.Parent or Char
		local player = game.Players:GetPlayerFromCharacter(character)

		----------- Punch -------------------------
		if Input == Enum.UserInputType.MouseButton1 then
			if PunchDebounce == false  then
				PunchDebounce = true

				local PunchHitbox, connected = HitboxClass.new(PunchHitboxParams)

				PunchHitbox.WeldTo(PunchHitbox, Char.HumanoidRootPart, CFrame.new(0.25, 0.5, -4.5))

				PunchAnimation:GetMarkerReachedSignal("StartTheHitbox"):Connect(function()
					PunchHitbox:Start()
				end)
				PunchAnimation:GetMarkerReachedSignal("StopTheHitbox"):Connect(function()

					PunchHitbox:Destroy()

				end)

				PunchHitbox.HitSomeone:Connect(function(Char)
					for _, Model in ipairs(Char) do
                        print(Char)
						------- Stuff --------

					end
				end)

				PunchAnimation:Play()

				PunchAnimation:AdjustSpeed(1.5)
				PunchAnimation.Stopped:Wait()

				task.wait(.5)
				PunchDebounce = false
			end
		end
	end
end)

Using Stop():
Several hitboxes are created here so that the player can spot the npc multiple times, because these hitboxes stack.

Using Destroy():
Here the npc is only detected once per hitbox created, but the error appears.

The error is probably in my code, but I don’t know how to solve the problem so that it works correctly.

You are creating a hitbox every time the player clicks m1, I believe the HitboxClass is meant to create 1 hitbox, start when you need and stop when you don’t need. Creating and destroying hitbox frequently might be the cause of the issue.

local PunchHitbox, connected = HitboxClass.new(PunchHitboxParams)

			PunchHitbox.WeldTo(PunchHitbox, Char.HumanoidRootPart, CFrame.new(0.25, 0.5, -4.5))

			PunchAnimation:GetMarkerReachedSignal("StartTheHitbox"):Connect(function()
				PunchHitbox:Start()
			end)
			PunchAnimation:GetMarkerReachedSignal("StopTheHitbox"):Connect(function()

				PunchHitbox:Stop()

			end)

			PunchHitbox.HitSomeone:Connect(function(Char)
				for _, Model in ipairs(Char) do
                    print(Char)
					------- Stuff --------

				end
			end)

Relocate this part right after Animations section, Also change :Destroy to :Stop

I don’t think this is necessary because you already have Player and Char defined at the top

Thank you so much!

I followed the steps you said and now the Hitbox is only created once and called when necessary.

I can’t believe that all I had to do was move the lines of code related to the Hitbox and the animation events outside of the Remote Event.

thanks and have a nice day

1 Like

Hey I have a big problem with the module idk when exactly this happens but in some cases characters dont get detected well one of the cases is if they get spawend in (like if I spawn a dummy) after 5 secounds after the playtest started the same is for all players and when a dummy respawns so I dont think its anything with my code but rather something with the module which is maybe buggy (btw all characters are in the alive folder)

Watch 2024-05-20 22-52-15 | Streamable --this link expires in 2 days if you need a new one pls ask

this is a part of my script the one that handles the normal punch:
module.normalPunch = function (char,hum)

COMBO = char:GetAttribute("combo")
changeCombo(char)

task.spawn(function () sh.jump(char,0.6) end)

local newHitbox, connected = HitboxClass.new(hitboxParams)

local hits = {}
newHitbox.HitSomeone:Connect(function(hitchars)
	for m,i in hitchars do
		local echar = hitchars[m]
		local eHum = echar.Humanoid

		local Isblocking = bm.IsBlocked(echar,char)

		if bm.Blocking == true then 
			sound(script.BlockedHit,echar.HumanoidRootPart)	
		return end
		if echar == char then return end
		if echar.IsRagdoll.Value == true then return end
		if eHum.Health == 0 then return end

		hits[#hits+1] = eHum
		PlayHitAnim(eHum)

		sound(script.Phit,echar.HumanoidRootPart)

		eHum:TakeDamage(3)
		pushForce(char,echar,7)

		for _,i in hits do
			if i then
				if game.Players:GetPlayerFromCharacter(char):DistanceFromCharacter(echar.HumanoidRootPart.Position) >= 5.5 then
					pullForce(char,echar,12)
				elseif game.Players:GetPlayerFromCharacter(char):DistanceFromCharacter(echar.HumanoidRootPart.Position) >= 1.5 then
					pullForce(char,echar,7)
				end
			end
		end

		task.spawn(function() sh.stunned(echar,1) end)
	end
end)

HitboxClass.WeldTo(newHitbox,char.HumanoidRootPart,CFrame.new(0,0,-2))
char:SetAttribute("canblock",char:GetAttribute("canblock") + 1)
newHitbox:Start()
wait(0.4)
newHitbox:Stop()
task.spawn(function() wait(0.5) char:SetAttribute("canblock",char:GetAttribute("canblock") - 1) end)

end

Blockquote

and if you know what the problem is or if you have the same PLS tell me thx for everyone : )

I downloaded this but I still have the problem

Hey, I just wanted to point out that there are players who can delete the WorkSpace folder alive by manipulating it. Is this really the best way to pack all the players into it?

“They can delete alive folder”. Yes, but that doesn’t do ANYTHING to the server if you are using RIGHT WAY(ServerSide). If they delete Alive folder, they will stop seeing another players and NPC, so they can not see them but other Players and NPC can see them and by deleting Alive folder they are making game harder ONLY FOR THEM. I mean they can delete walls and no clip through them. Everything can be safe from exploits if you will not be lazy. I saying this for you to understand that the “deleting Alive folder” will not give exploiters ANY advantages

1 Like