Damage Dealt is multiplied by each player hit?

I’m writing a script in which a blast is fired. Upon the blast being fired, it checks for if it touches any player and if it does, it spawns a hitbox that deals damage 20 to the player then deletes itself.
The problem is the fact that when the blast hits 2 people at once within said radius, it deals 40 damage to both players. If it hits 3 players at once, it deals 60 damage to all of the players hit. I have written another similar detection script to this, however it does not have any issues with dealing multiplied damage to all players.
This is the faulty script.

		Blast.Touched:Connect(function()
			local PartsHitByAttack = workspace:GetPartsInPart(Blast)
			print(PartsHitByAttack)
			for i, Part:Part in pairs(PartsHitByAttack) do
				local TargetCharacter:Model = Part.Parent
				local TargetHumanoid:Humanoid = TargetCharacter:FindFirstChildOfClass("Humanoid")
				if not table.find(AlreadyHit, TargetCharacter) and TargetHumanoid and TargetCharacter ~= Character then
					table.insert(AlreadyHit, TargetCharacter)
					DebrisService:AddItem(Blast, 0)
					Hitbox.Position = Blast.Position
					Hitbox.Parent = workspace
					print(AlreadyHit)
					InflictDamage(Hitbox, Character, Damage, Knockback, PostureDamage, Blockable, Dodgeable, Parryable)
				end
			end
		end)

This is the nonfaulty script:

	local AlreadyHit = {}
	Hitbox.Touched:Connect(function()
		for i, Part:Part in pairs(PartsHitByAttack) do
			local TargetCharacter:Model = Part.Parent
			if not table.find(AlreadyHit, TargetCharacter) and TargetCharacter ~= nil and TargetCharacter:FindFirstChildOfClass("Humanoid") and TargetCharacter ~= Attacker then
				local TargetHumanoid:Humanoid = TargetCharacter:FindFirstChildOfClass("Humanoid")
				table.insert(AlreadyHit, TargetCharacter)```
1 Like

you can copy and paste the code directly.
surround it with ```

The code was copy and pasted and had things changed to fit the situation.

i mean you shall copy and paste the code ON THE FORUM directly,
it will be more convenient for others to read the code as text, rather than needed to enlarge the image. and other dev can quote / copy and paste your code easier.

Alright, I just did that, take a look at the code.

1 Like

i notice you used both workspace:GetPartsInPart(Blast) and Blast.Touched event
they both do the same thing but because you mixed them up, it will cause bug that depends on conditions.

i doubt the Hitbox would also work correctly, but i’ll just explain and post the correct method that I would suggest:

local AlreadyHit = {}
Hitbox.Touched:Connect(function(Part)
	local TargetCharacter:Model = Part.Parent
	if not table.find(AlreadyHit, TargetCharacter) and TargetCharacter ~= nil and TargetCharacter:FindFirstChildOfClass("Humanoid") and TargetCharacter ~= Attacker then
		local TargetHumanoid:Humanoid = TargetCharacter:FindFirstChildOfClass("Humanoid")
		table.insert(AlreadyHit, TargetCharacter)
...

Touched would fire on EACH part that in touch with the Hitbox, so make sure we get the part passed in function(Part). We don’t need the GetPartsInPart if we use Touched

On the other hand, this is when we use GetPartsInPart instead

local AlreadyHit = {}
local function CheckHit()
	local PartsHitByAttack = workspace:GetPartsInPart(Hitbox)
	for _, Part:Part in PartsHitByAttack do
		local TargetCharacter:Model = Part.Parent
		if not table.find(AlreadyHit, TargetCharacter) and TargetCharacter ~= nil and TargetCharacter:FindFirstChildOfClass("Humanoid") and TargetCharacter ~= Attacker then
			local TargetHumanoid:Humanoid = TargetCharacter:FindFirstChildOfClass("Humanoid")
			table.insert(AlreadyHit, TargetCharacter)
			...
		end
	end
end

-- then we need to call this method at suitable moment, or every frame, e.g.
game:GetService("RunService").Heartbeat:Connect(CheckHit)

The hitbox works perfectly fine with no bugs. I used .touched in order to activate the :getpartsinpart rather than having to rely on runservice as I assumed that was not the best idea.
The .touched alternative you gave is even more random with calculating damage.

I figured out the issue, the usage of the alreadyhit code was pointless as it would destroy the blast anyways and it contributed no damage, and made it trigger the damage function twice.