The hitbox causes player to take damage multiple times,

I’m making a combat game and the issue is when the player character touches the hitbox, while they do take damage, it causes the players to take damage every second for each limb they have when I only want them to take damage once.



		local HitParts = workspace:GetPartBoundsInBox(Hitbox.CFrame, Hitbox.Size, overlapParams)
		for _, part in pairs(HitParts) do
			if not CanHit then return end
			local Humanoid = part.Parent:FindFirstChild("Humanoid")
			if Humanoid and not part:IsDescendantOf(Character) then
				local CombatValues = part.Parent:FindFirstChild("Values")
				if CombatValues and CombatValues.Combat.Blocking.Value == false then
					-- Check if player is already stunned
					local playerName = Player.Name
					if not Stunned[playerName] then
						Stunned[playerName] = true
						
						-- Perform stun action on enemy
						local EnemyChar = Humanoid.Parent
						local EnemyHum = Humanoid
						local EnemyHumRoot = part.Parent:FindFirstChild("HumanoidRootPart")
						local EnemyValues = EnemyChar.Values.Combat
						local EnemyStunValue = EnemyValues.Stunned

						CanHit = false

						EnemyHum:TakeDamage(BaseDamage)
						EnemyStunValue.Value = true

						local HitAnim = EnemyHum:LoadAnimation(EnHit)
						HitAnim:Play()
						delay(.5, function()
							HitAnim:Stop()
						end)

						local HitFX = HitEffect:Clone()
						HitFX.Parent = EnemyHumRoot
						HitFX:Emit(20)
						Debris:AddItem(HitFX, 1.5)

						local SoundFX = HitSound:Clone()
						SoundFX.Parent = EnemyHumRoot
						SoundFX:Play()
						Debris:AddItem(SoundFX, 1.5)

						EnemyHum.WalkSpeed = 0
						EnemyHum.JumpPower = 0

						wait(.6)
						CanHit = true
						EnemyStunValue.Value = false
						EnemyHum.WalkSpeed = 16
						EnemyHum.JumpPower = 10
					end
				end
			end
		end

		Hitbox.Parent = Ignore
		Debris:AddItem(Hitbox, .2)
		Debris:AddItem(Weld, .2)
	end
```end)
1 Like
-- Configuration for cooldown duration
local CooldownDuration = 1 -- Duration in seconds

-- Dictionary to keep track of cooldowns for players
local HitCooldowns = {}

local HitParts = workspace:GetPartBoundsInBox(Hitbox.CFrame, Hitbox.Size, overlapParams)
for _, part in pairs(HitParts) do
	if not CanHit then return end
	local Humanoid = part.Parent:FindFirstChild("Humanoid")
	if Humanoid and not part:IsDescendantOf(Character) then
		local CombatValues = part.Parent:FindFirstChild("Values")
		if CombatValues and CombatValues.Combat.Blocking.Value == false then
			-- Check if player is already stunned and not on cooldown
			local playerName = Player.Name
			if not Stunned[playerName] and not HitCooldowns[playerName] then
				Stunned[playerName] = true
				HitCooldowns[playerName] = true
				
				-- Perform stun action on enemy
				local EnemyChar = Humanoid.Parent
				local EnemyHum = Humanoid
				local EnemyHumRoot = part.Parent:FindFirstChild("HumanoidRootPart")
				local EnemyValues = EnemyChar.Values.Combat
				local EnemyStunValue = EnemyValues.Stunned

				CanHit = false

				EnemyHum:TakeDamage(BaseDamage)
				EnemyStunValue.Value = true

				local HitAnim = EnemyHum:LoadAnimation(EnHit)
				HitAnim:Play()
				delay(.5, function()
					HitAnim:Stop()
				end)

				local HitFX = HitEffect:Clone()
				HitFX.Parent = EnemyHumRoot
				HitFX:Emit(20)
				Debris:AddItem(HitFX, 1.5)

				local SoundFX = HitSound:Clone()
				SoundFX.Parent = EnemyHumRoot
				SoundFX:Play()
				Debris:AddItem(SoundFX, 1.5)

				EnemyHum.WalkSpeed = 0
				EnemyHum.JumpPower = 0

				wait(.6)
				CanHit = true
				EnemyStunValue.Value = false
				EnemyHum.WalkSpeed = 16
				EnemyHum.JumpPower = 10
				
				-- Remove cooldown after specified duration
				delay(CooldownDuration, function()
					HitCooldowns[playerName] = nil
				end)
			end
		end
	end
end

Hitbox.Parent = Ignore
Debris:AddItem(Hitbox, .2)
Debris:AddItem(Weld, .2)

try this. added a cool down

1 Like