Same player, damaged multiple times

The script detect parts that’s touching a hitbox only once and damage them if they belong to a Character. The problem here is if the hitbox is touching multiple parts of the same character, the character will be damage multiple time. I’m trying to find a way to make it only damage the same character once.

hitboxPart.Touched:Connect(function()
		end)
		for i, v in pairs(hitboxPart:GetTouchingParts()) do
			local targetHmd = v.Parent:FindFirstChildOfClass("Humanoid")
			if targetHmd and v.Parent ~= hmd.Parent then
				targetHmd:TakeDamage(10)
			end
		end
3 Likes

Check to see if it is only hitting a specified part of the players character. Or you could also add a debounce.

Checking if it’s only hitting a specified part would work but if in the future if i add some custom rig, it would be inconvenient for the player to hit that specified part

Are you actually using the touched event?

You should use a table containing the parent of the parts you’ve just hit. When your hitbox touch a part, use table.find() to check if the parent of this part is in the table. If it’s not, the script deals damages.

local PartsHitten = {}
hitboxPart.Touched:Connect(function()
		end)
		for i, v in pairs(hitboxPart:GetTouchingParts()) do
			local targetHmd = v.Parent:FindFirstChildOfClass("Humanoid")
			if targetHmd and v.Parent ~= hmd.Parent then
               if not table.find(PartsHitten, targetHmd.Parent) then
                table.insert(PartsHitten, targetHmd.Parent)
				targetHmd:TakeDamage(10)
wait(2)
table.remove(PartsHitten, table.find(PartsHitten, targetHmd.Parent))
                          end
			end
		end
1 Like

this is the wried script ever i seen

local hitboxPart = *your part here*

hitboxPart.Touched:Connect(function()
	for i, v in pairs(hitboxPart:GetTouchingParts()) do
		local targetHmd = v.Parent:FindFirstChildOfClass("Humanoid")
		if targetHmd and v.Parent ~= hmd.Parent then
			targetHmd:TakeDamage(10)
		end
	end
end)

You could add the player to a temporary dictionary to prevent them from getting attacked again. For example-

local attackedAlready = {}

...

hitboxPart.Touched:Connect(function()
	for i, v in pairs(hitboxPart:GetTouchingParts()) do
		local targetHmd = v.Parent:FindFirstChildOfClass("Humanoid")
		if targetHmd and v.Parent ~= hmd.Parent and game.Players:playerFromCharacter(v.Parent) then
			if alreadyAttacked[v.Parent.Name] == nil then
				alreadyAttacked[v.Parent.Name] = true
				targetHmd:TakeDamage(10)
				wait(1)
				alreadyAttacked[v.Parent.Name] = nil
			end
		end
	end
end)

I hope this helped :ok_hand:

1 Like

Tried it but the i, v in pairs() loop makes it loop through every value in the table so the dummy ended up taking the same amount of damage but with a delay this time.

tried it but didnt work, but when i removed the and game.Players:playerFromCharacter(v.Parent) from the script, it seems to work butthe dummy takes the same amount of damage with a delay between each one and it seems to pause the script (also im testing on dummies, dont know if itll work on player)

yea i am, its the same when you put the index, value loop inside of the touched function

local Debounce = {}		
hitboxPart.Touched:Connect(function(hit)
    local Character = hit.Parent
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    
    if Character and Humanoid.Health > 0 and Character ~= hmd.Parent then
        if not Debounce[Character] then
            Debounce[Character] = true
            Humanoid:TakeDamage(10)
            task.wait(1)
            Debounce[Character] = nil
        end
    end
end)

Found the solution, instead of removing the target from the table inside of the loop. I do table.clear outside of the index, value loop, seems to work