Table.remove not removing args

Hello everyone. I have been working on a script which requires the use of table.remove alot. I decided to work on a sort of table.remove mass function which grabs all the arguments and removes them. However when I use it only 2 out of the 4 args are removed. I have been working on it but I can’t seem to find the issue. Any help would be great.

local Haircuts = {"DandyTrim",
	"StarTrim",
	"LaReineTrim",
	"KabukiTrim",
	"PharoahTrim",
	"MatronTrim",
	"DebunteTrim",
	"HeartTrim",
	"DiamondTrim" 
}

local function  ToRemove (tbl,...)
	
local args = {...}	
	
	for i,v in ipairs(tbl) do
		if table.find(args,v) then
			print(v)
			--only finds 2 matching
			table.remove(Haircuts,i)
		end
	end
	
end

ToRemove(Haircuts,"MatronTrim","DebunteTrim","HeartTrim","DiamondTrim" )

warn(#Haircuts) --only 2 removed out of 9 even after we gave 4 arguments

I switched the order of tbl and args from this

local function  ToRemove (tbl,...)
	
    local args = {...}	
	
	for i,v in ipairs(tbl) do
		if table.find(args,v) then
			print(v)
			--only finds 2 matching
			table.remove(Haircuts,i)
		end
	end
end

To this

local function  ToRemove (tbl,...)
	
    local args = {...}	
	
	for i,v in ipairs(args) do
		if table.find(tbl,v) then
			print(v)
			--only finds 2 matching
			table.remove(Haircuts,i)
		end
	end
end

And it seemed to have properly detected all 4

image

Edit: Hang on let me check the table cause I think that bit might be a bit messed up since I didn’t check the table, if it’s messed up the table, I’ll find a fix for it

Okay yea it messed u pthe table, use this

local Haircuts = {"DandyTrim",
	"StarTrim",
	"LaReineTrim",
	"KabukiTrim",
	"PharoahTrim",
	"MatronTrim",
	"DebunteTrim",
	"HeartTrim",
	"DiamondTrim" 
}

local function  ToRemove (tbl,...)
	
local args = {...}	
	
	for i,v in ipairs(args) do
		local found = table.find(tbl,v)
		if found then
			print(v)
			table.remove(Haircuts,found)
		end
	end
	
end

ToRemove(Haircuts,"MatronTrim","DebunteTrim","HeartTrim","DiamondTrim" )

warn(#Haircuts)

table.find returns the index at which it found something in a table or nil if it did not find it, so you can use that to get the index

1 Like

Wow, your fix was better than what I was going to send haha

Wait really? What were you going to originally send? You got me curious haha

I wrote a tablefind function instead of using the built in one. Also couldn’t he just have done like

for i,v in ipairs(args) do
    table.remove(Haircuts, table.find(tbl,v))
end

Ah nevermind, it might throw an error if its nil

I tried that out, giving an invalid thing in the table, but it was still deleting 4 when 3 were found, I think if you give it nil, it treats it as if you were trying to get rid of the last thing in the table. I added a random thing at the end and then fired the function without mentioning the thing I added, it still deleted it

Edit: Yea it treats nil as the last thing in the table

image

1 Like