Help making hitbox

hello, i need help making hitboxes to my game and right now the hitbox runs many times at once and does alot of damage, so i would like some help to fix that.

heres my script:

		local Debris = game:GetService("Debris")
		local humRP = player.Character.HumanoidRootPart
		local TweenS = game:GetService("TweenService")
		local DamageP = Instance.new("Part",workspace)
		local damage = 12 * player.FLvl.Value
		local Table = {}
		DamageP.Size = Vector3.new(40,40,40)
		DamageP.Anchored = true
		DamageP.CanCollide = false
		DamageP.Transparency = 1
		DamageP.CFrame = humRP.CFrame
		Debris:AddItem(DamageP,.35)
		DamageP.Touched:Connect(function(hit)
			if not hit:IsDescendantOf(player.Character) then 
				if hit.Parent:FindFirstChild("Humanoid") then 	
					table.insert(Table,hit.Parent.Name)
					for i,v in pairs(Table) do
						hit.Parent.Humanoid.Health -= damage
						local damageI = game:GetService("ReplicatedStorage").DmgIndicator:Clone()
						damageI.Parent = hit.Parent.HumanoidRootPart
						damageI.TextLabel.Text = "-"..damage
						damageI.StudsOffset = Vector3.new(math.random(-2,2),math.random(-2,2),math.random(-2,2))
						local TweenDmg = TweenS:Create(damageI,TweenInfo.new(1,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out, 0, false), {Size = UDim2.new(2,0,2,0)})
						TweenDmg:Play()
						TweenDmg.Completed:Connect(function()
							local TweenDmg2 = TweenS:Create(damageI,TweenInfo.new(1,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out, 0, false),{Size = UDim2.new(0,0,0,0)})
							TweenDmg2:Play()
						end)
					end														
				end
			end	
		end)

First, you’re running for i’v in pairs() which will loop as many times as the function based on the amount of instances inside the table. Second, why would you use hitbox based on .Touched, isn’t it better to use Raycast Hitbox? The .Touched hitbox doesn’t work great at all really

the thing with the raycast, how would i make the racast go all directions if needed?

What do you mean by going all directions?

because of the vector3 it can go x,y and z but how would i make it go in minus

Wait, what do you want to achieve exactly?

i want to make a hitbox that goes around the player in all directions

I still don’t really understand you. Do you want to make 360 melee hitbox or smthing?

ye kinda like that i guess 30 letters

How does your script works? Like you’re pressing certain button and a hitbox appear around the player basically?

yes aaaaaaaaaaaaaaaaaaaaaaaaaa

I guess you can just keep the .Touched hitbox then lol, no need in raycasting really

Try adding a debounce table so it will work for multiple players, but will not fire for a player more than once until they are removed from the table.

local db = {}
script.Parent.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
if not table.find(db,player) then
table.insert(db,plr)
-- your damage code here
task.wait(3)
-- remove them from the debounce table after a specified amount of time
end
end
end)

(Sorry for the bad indentation, typing the script in this text box doesn’t let me indent for some reason)

1 Like

ok aaaaaaaaaaaaaaaa

Instead of using .Touched, please just use workspace:GetPartsBoundInRadius(). You do not even need to run it multyple times, just once for an instantaneous hitbox. If you need to make it active just wrap it inside a while loop. Either way, .Touched runs to many times usually.

Lastly, to prevent hitting too many times (hitting various parts of the player) you can have a table “blacklist” the character. For example, I call the function, the first part is the leg, I grab the first ancestor which is a model, add it to a table, and continue with the loop. Next time I check a part, I once more will grab the model and use table.find to check if it is in the mentioned table. Hopefully that helps!

Another good method is to just check if it is the HumanoidRootPart.

and yes, OP should not be using .Touched

Or even attach a new hitbox to the characters in case op ever wants to extend or such. In that situation, though, an anti-exploit would have to be implemented.

hey, could you give me a little more description on how to use workspace:GetPartsBoundInRadius()? and how it works?

Just curious why an anti-exploit would have to be developed in that scenario, and not even with the HumanoidRootPart?

Maybe I’m not getting what you mean here.

workspace:GetPartsBoundInRadius(Vector3.new(0,0,0), 5, OverlapParams{MaxParts=false, BruteForceAllSlow=false, CollisionGroup=Default, FilterDescendantsInstances={}})

And it returns a table of objects.

So the position is the center I believe, and then radius is the distance it checks. I’ve never used so I may be wrong

Copied params from developer hub lol