Table.find Not Working Correctly

Hi! I’ve been trying to create a damage module for my fighting game, but the damage cooldown part doesn’t seem to work. It’s supposed to search for the player in the debounce table, and if it doesn’t find it, it’s supposed to damage the player and some other stuff. Even though the player does get inserted into the table, the first if statement still runs.

The issue is that the first if statement runs even though the player is in the table. I’ll add a video to demonstrate.

I have had a difficult time finding any information on the topic, so I decided to post it here.

Here’s the code:

local dmg = {

	takedmg = function(hit, damage, level, cooldown, color)
		local debounce = {}
		if not table.find(debounce, hit.Parent) then
			if color == nil then
				color = Color3.new(0.7,0,0)
			end
			hit.Parent.Humanoid.Health -= damage + (damage / 10) * level
			print("was2")
			table.insert(debounce, hit.Parent)
			print("is1")
			local counter = Instance.new("BillboardGui")
			counter.Parent = hit.Parent.HumanoidRootPart
			local much = Instance.new("TextLabel")
			much.Parent = counter
			much.TextColor3 = color
			much.Text = (damage + damage / 10) * level
		end
		if table.find(debounce,hit.Parent) then
			print("is")
			task.wait(cooldown)
			table.remove(debounce, table.find(debounce, hit.Parent))
		end
	end
}

return dmg

Here’s the video:

move the debounce table outside of the takedmg function because everytime you call the function youre creating a new empty table

2 Likes

While that didn’t work, you made me realize that a table WAS being created every time the function was called, so I added something like this, and it worked:

-- bing = false

if bing == false then
			debounce = {}
			bing = true
		end

Thanks alot!

2 Likes