Need some help with arrays

Here’s a script that adds a Character to a table everytime the Character.Humanoid takes damage(which stops it from taking damage again until the character is removed from the table)

local module = {}

local Damage = require(script.Parent.Damage) -- damage module

local RMV = function(Time,Array,Character)
	wait(Time)
	table.remove(Array,Character) -- doesn't work
end

module.HitBox = function(Character, Hit, DamagedArray, Damage, RmvArray)
	if Hit.Parent == Character then -- you don't want to damage your own character
		return
	end
	local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if Humanoid == nil then
		return
	end
	local EnemyCharacter = Hit.Parent
	if table.find(DamagedArray, EnemyCharacter) == nil then
		table.insert(DamagedArray,EnemyCharacter)
		if RmvArray then
			RMV(RmvArray,Damaged,EnemyCharacter)
		end
		Damage.Damage(EnemyCharacter,Damage)
		return true
	end
end

return module

I want the RMV function to remove the EnemyCharacter from the DamagedArray. But i can’t use table.remove, since the index number is unpredictable. What’s the easiest way to get the desired result? i want to keep the scrip simple and efficient

Use table.find to find the index.

local index = table.find(Array, Character)
if index then
	table.remove(Array, index)
end
1 Like