Help with checking if a player has touched a part before

Hi, developers!

I’ve been trying to code a boss fight for a few days and just recently, I ran into a problem where a player can get hit multiple times by a melee, despite it only supposed to be happening once.

I have a check to see if players are in a debounce table, and if they aren’t, put the player in that table after they’ve been hit. A bit after the attack, clear the table. I checked console and printed the character, and it printed 6 times.

Here’s my code:

if not table.find(debounces,player) then
				player.Character.Humanoid.Health -= meleeDmg
				local newimpact = sounds.SlashImpact:Clone()
				newimpact.Parent = model.Weapons.Sword.Hitbox
				newimpact:Play()
				game:GetService('Debris'):AddItem(newimpact,newimpact.TimeLength)
				table.insert(debounces,player)

Any help would be appreciated!

2 Likes

How much later after the attack and can you show me where you clear the table.

1 Like

Here’s the rest of the actual attack:

task.wait(0.66)
	newsound:Play()
	connection =	model.Weapons.Sword.Hitbox.Touched:Connect(function(hit)
		print(hit.Parent.Name)
		if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not table.find(debounces,player) then
				player.Character.Humanoid.Health -= meleeDmg
				local newimpact = sounds.SlashImpact:Clone()
				newimpact.Parent = model.Weapons.Sword.Hitbox
				newimpact:Play()
				game:GetService('Debris'):AddItem(newimpact,newimpact.TimeLength)
				table.insert(debounces,player)
				
				for i, plr in pairs(debounces) do
					print(plr)
				end
			connection:Disconnect()
			end
		end
	end)
	model.Humanoid.WalkSpeed = statFolder.Speed.Value
	task.wait(1.35)
	table.clear(debounces)
1 Like

To better clarify, I clear the table 1.35 seconds after he attacks.

1 Like

You can make a LocalScript with a boolean that checks if the attack has taken place before. (This way it doesn’t effect all players)

1 Like

I could try that, but what’s weird is the check was working just fine before a few changes ago.

1 Like

~ Use overlapParams for hitboxes ~


example:


local part = -- hitbox
local overlaparams -- ignore parts

workspace:GetPartsInPart(part,overlaparams)

overlapsparams tutorial:


local character = game.Players.LocalPlayer.Character

local hitboxOverlapParams = OverlapParams.new()
hitboxOverlapParams.FilterType = Enum.RaycastFilterType.Exclude -- Exclude its old blacklist
hitboxOverlapParams.FilterDescendantsInstances = {character} -- blacklist parts

Good Luck!


1 Like

Seeing I’ve never used overlapParams before, I’m still trying to figure out how to log new characters into the descendants table. I’ve made a few quick changes but I get an error at line 159, or the table.clear() line. Here’s the readjusted code:

if not table.find(overlapparams.FilterDescendantsInstances,hit.Parent) then
				player.Character.Humanoid.Health -= meleeDmg
				local newimpact = sounds.SlashImpact:Clone()
				newimpact.Parent = model.Weapons.Sword.Hitbox
				newimpact:Play()
				game:GetService('Debris'):AddItem(newimpact,newimpact.TimeLength)
				table.insert(overlapparams.FilterDescendantsInstances,hit.Parent)
			connection:Disconnect()
			end
		end
	end)
	model.Humanoid.WalkSpeed = statFolder.Speed.Value
	task.wait(1.35)
	table.clear(overlapparams.FilterDescendantsInstances)
	end

Good thing that came out of it was that my player did only get hit once.

1 Like
local hitParts = workspace:GetPartsInPart(hitBox, hitBoxOverlapParams)

local alreadyHit = {}

if #hitBox ~= 0 then
	for i,v in hitParts do
		local humanoid = v.Parent:FindFirstChild("Humanoid")
		if humanoid then
			if not table.find(alreadyHit,humanoid) then
				humanoid:TakeDamage(10)
				table.insert(alreadyHit,humanoid)
			end	
		end
	end
end
local function melee()
	
	model.Humanoid.WalkSpeed = 0
		attacked.Value = true
		local newsound = slash:Clone()
		newsound.Parent = model.Weapons.Sword
		game:GetService('Debris'):AddItem(newsound,newsound.TimeLength)
		sliceAnim:Play(0.1,9,1)

	local connection
	
	task.wait(0.66)
	if model.Humanoid.Health > 0 then
	newsound:Play()
		connection =	model.Weapons.Sword.Hitbox.Touched:Connect(function(hit)
			
		print(hit.Parent.Name)
		if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				local hitbox = model.Weapons.Sword.Hitbox
				local hitboxParams = OverlapParams.new()
				hitboxParams.FilterType = Enum.RaycastFilterType.Exclude
				hitboxParams.FilterDescendantsInstances = {hit.Parent}
			
				local hitParts = workspace:GetPartsInPart(hitbox,hitboxParams)
				
				local alreadyHit = {}
				
				if #hitParts ~= 0 then
					for i,v in hitParts do
						local humanoid = v.Parent:FindFirstChild('Humanoid')
						if humanoid then
							if not table.find(alreadyHit,humanoid) then
								local newImpact = impact:Clone()
								newImpact.Parent = model.Weapons.Sword
								newImpact:Play()
								game:GetService('Debris'):AddItem(newImpact,newImpact.TimeLength)
								humanoid:TakeDamage(meleeDmg)
								table.insert(alreadyHit,humanoid)
								connection:Disconnect()
							end
						end
					end
				end
				model.Humanoid.WalkSpeed = statFolder.Speed.Value
				task.wait(1.35)
				table.clear(alreadyHit)
end

Did the best of my ability to try to incorporate your sent script above. Everything gets checked once, but no damage is being dealt to the player.

Update: nevermind it doesnt check once

ok, so be it, I’ll write a full script for you


Script:


local combat = -- tool
local Debris = game:GetService("Debris")
local hitBoxLifeTime = .05

combat.Activated:Connect(function()
    local hitBox -- hitbox
    local hitBoxOverlapParams = -- blacklist files

    local hitParts = workspace:GetPartsInPart(hitBox, hitBoxOverlapParams)
    local alreadyHit = {}
    Debris:AddItem(hitBox, hitBoxLifeTime)
    if #hitBox ~= 0 then
           for i,v in hitParts do
		local humanoid = v.Parent:FindFirstChild("Humanoid")
		if humanoid then
			if not table.find(alreadyHit,humanoid) then
                                Humanoid:TakeDamage(meleeDmg)
				local newimpact = sounds.SlashImpact:Clone()
				newimpact.Parent = model.Weapons.Sword.Hitbox
				newimpact:Play()
				game:GetService('Debris'):AddItem(newimpact,newimpact.TimeLength)
				table.insert(alreadyHit,humanoid)
			end	
		end
	end
    end
end)

this should work!


customize it for your system