Damage layering over other hitboxs, in combat script

I’m currently making a combat system for my zombie defense game. However, the hitboxes that are generated from my server script seem to be stacking damage.



As you can see in the videos. When I attack but don’t hit anything my damage stacks, as if the hit box was still there.
This is my module in the server script service.

module.GenerateHitBox = function(Root, Anchored, Size, Cframe, LifeTime)
	local NewBox = Instance.new("Part",workspace)
	NewBox.Size = Size
	NewBox.CanCollide = false
	NewBox.Material = Enum.Material.ForceField
	NewBox.Color = Color3.new(1, 0.25098, 0)
	if Anchored == true then
		NewBox.Anchored = true
		NewBox.CFrame = CFrame
	else
		local NewWeld = Instance.new("Weld",Root)
		NewWeld.Part0 = NewBox
		NewWeld.Part1 = Root
		NewWeld.C0 = Cframe
	end
	if LifeTime then
		Debris:AddItem(NewBox,LifeTime)
	end
	return NewBox
end

And here is my code in the local script for input

local CanHit = true
	local HitBox1 = RemoteEvents.CreateHitBox:FireServer(RootPart,false,Vector3.new(1,1,3),CFrame.new(1.1,-0.8,2) * CFrame.Angles(math.rad(10),math.rad(40),0),0.1)
	RemoteEvents.CreateHitBox.OnClientEvent:Connect(function(Part)
	Part.Touched:Connect(function(hit)
	if hit and hit.Parent and CanHit == true then
		local TargetHum = hit.Parent:FindFirstChild("Humanoid")
		local TargetHumRoot = hit.Parent:FindFirstChild("HumanoidRootPart")
		if TargetHum and TargetHumRoot then
			RemoteEvents.ApplyForce:FireServer(TargetHumRoot, RootPart, RootPart.CFrame.LookVector*6 + RootPart.CFrame.RightVector*2, 6, 0.4)				AnimationStack = TargetHum:LoadAnimation(Animations:WaitForChild("NormalAtk"):WaitForChild("LightArmAtk"):WaitForChild("Attack2"):WaitForChild("Stagger1"))
			--AnimationStack:Play()
			RemoteEvents.DamageEntity:FireServer(TargetHum,10)
			CanHit = false
			AttackConnected = true
			end
		end
	end)
end)

Does anyone know why the damage is stacking? If you need any more info do ask.

You need to add a debounce on the server. This will also prevent exploiters from spamming.

2 Likes

The damage might be stacking because you are generating hit boxes on every hit, and the hit boxes don’t go away after the hit is over. This would cause an event to be fired for each hitbox that is generated once the hitboxes touch an entity.

1 Like

The hit box is generated before the hit, and the hit boxes have a life time on the server using debris.

Adding a debounce on the server would prevent other players from using hitbox gen at the same time, it would also not help that much.

Try to set these variables before firing the Remote Events.

That did not seem to work, any other ideas?

Just to confirm, the parts that are created are getting deleted after their life span has ended.

Did you add if not CanHit then return end before firing the event?

In the script i have a line of code that says

if hit and hit.Parent and CanHit == true then

which is checking if the hitbox has already been fired. Also, my hit boxes aren’t hitting multiple times, there taking the damage from the hit boxes that didn’t land, and are adding the damage together. For some reason…

This must not be the problem then. Check how many hitboxes you have made.

I keep checking the workspace for error parts but, they all seem to be deleted. But I put a print function in the damage code on the server script, and it seems to print multiple even though only one should print. What should I do?