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
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)
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!
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.