GetTouchingParts hitting people multiple times

I’m trying make it so that when multiple people get hit ‘once’, but instead it hits multiple people more than once and if I add a return end after being damaged it only hits 1 player

The code:

spawn(function()
			local Parts = GetTouchingParts(hitbox)
			for i,v in pairs(Parts) do
				if v.Parent:FindFirstChild('Humanoid') or v.Parent.Parent:FindFirstChild('Humanoid') then
					local EnemyHumanoid = v.Parent:FindFirstChild('Humanoid')
					local EHumRP = v.Parent:FindFirstChild('HumanoidRootPart')
					if EnemyHumanoid then
						local EnemyCharacter = EnemyHumanoid.Parent
						local behindd = false
						if (char.HumanoidRootPart.Position - EnemyCharacter.HumanoidRootPart.CFrame * CFrame.new(0,0,-4.75).Position).magnitude < 5.5 then
							behindd = true
							end
						if EnemyHumanoid then
							local EnemyCharacter = EnemyHumanoid.Parent
							if EnemyCharacter ~= char then

								if EnemyCharacter:WaitForChild("IsBlocking").Value == true and behindd == true then
									local Sound = game.ReplicatedStorage.Sounds:FindFirstChild("BlockHit"):Clone()
									Sound.Parent = EnemyCharacter.HumanoidRootPart
									Sound.Pitch = 1 * (math.random(75, 100) / 100)
									Sound:Play()
								else
									if char:FindFirstChild("Stun") then return end
									EnemyHumanoid:TakeDamage(dmg)
									Damaged = true

									local Slowdown2 = Instance.new("NumberValue")
									Slowdown2.Name = "SpeedBoost"
									Slowdown2.Value = -EnemyCharacter.Humanoid.WalkSpeed * .7
									Slowdown2.Parent = EnemyCharacter.Effects
									game.Debris:AddItem(Slowdown2,.55)
									create("Stun", .55, EnemyCharacter)
									local Sound = game.ReplicatedStorage.Sounds:FindFirstChild("Punched"..math.random(1,5)):Clone()
									Sound.Parent = EnemyCharacter.HumanoidRootPart
									Sound.Pitch = 1 * (math.random(75, 100) / 100)
									Sound:Play()
									local ANIMATION = EnemyCharacter.Humanoid:LoadAnimation(anims.DefaultAnims:FindFirstChild("Stun"..math.random(1,3)))
									for i,v in pairs(game.Players:GetChildren()) do
										if v.Character and v.Character:FindFirstChild("HumanoidRootPart") then
											local mag = (root.Position - v.Character.HumanoidRootPart.Position).magnitude
											if mag < 5 then
												game.ReplicatedStorage.screenShake:FireClient(v,2.5,14)
											end
										end
									end
									ANIMATION:Play()
									ANIMATION:AdjustSpeed(2)
									local PunchEffect = EHumRP.Blunt.Hit
									PunchEffect:Emit(75)
								end
							end
						end
					end
				end
			end
			
		end)
	end
1 Like

You will also need to check for the model like in this example I code I stole from the explosion example code.

Instead of explosion.Hit you can just use you for loop which does the detection already.

local function customExplosion(position, radius, maxDamage)
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 0 -- this could be set higher to still apply velocity to parts
	explosion.DestroyJointRadiusPercent = 0 -- joints are safe
	explosion.BlastRadius = radius
	explosion.Position = position
	
	-- set up a table to track the models hit
	local modelsHit = {}
 
	-- listen for contact
	explosion.Hit:Connect(function(part, distance)
		local parentModel = part.Parent
		if parentModel then 
			-- check to see if this model has already been hit 
			if modelsHit[parentModel] then
				return
			end
			-- log this model as hit
			modelsHit[parentModel] = true
 
			-- look for a humanoid
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
				local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
				distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
				humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
			end	
		end
	end)
 
	explosion.Parent = game.Workspace
end
1 Like