Table.remove not working

I am trying to make a debounce for a special ability (debounce is on the server so it is exploiter proof), and I am running into issues with table.remove. my debounce currently works because when you use the ability your put into a table, and then later when your debounce is over you are removed. here is my script:

			if not table.find(Debounce, Plr.UserId) then
				table.insert(Debounce, Plr.UserId)

				--run ability

				wait(10)
				table.remove(Debounce, Plr.UserId)
			end

I am 100% sure table.remove is the problem, this is because when I add a print(Debounce[1]) right before the if statement it prints nil the first time, and then when I wait 10 seconds and run it again it prints my user ID, showing it never got removed.

Try the following:

           if not table.find(Debounce, Plr.UserId) then
				table.insert(Debounce, Plr.UserId)

				--run ability

				task.wait(10)
				table.remove(Debounce, Debounce[Plr.UserId]
           end
1 Like

When you are using table.remove , you’ll need to access your desired thing by indicating it from the table("[]")

1 Like

if that is the case, wouldn’t table.remove(Debounce, Debounce[Plr.UserId]) not work? the index of the user ID is not the user ID, it is the place in the table it is, right?

Did you test it out? Use prints to check what does it print

nevermind, it works! Im a little confused why, can you use the entry in the table as the index?

local T = {
1234
"test"
}

table.remove(T, T[1234])
table.remove(T, T["test"])

would this clear the table?

1 Like

Yes, it will.

Just don’t forget to separate them with a comma.

local T = {
	1234,
	"test",
}

table.remove(T, T[1234])
table.remove(T, T["test"])


print(T[1]) -- nil
print(T[2]) --nil

Yes, Indeed that would remove everything out of the table.

local T = {
	1234,
	"test"
}

warn(T)
table.remove(T, T[1234])
table.remove(T, T["test"])
warn(T)

run the above code

Notice,

for e.g :

the ‘1234’ and ‘test’ are found in your table, hence they were removed. If you put ‘252’ it still would print 1234.

cool, I didn’t know you could index a table with the entry. Is this only in table.remove, or does it work to index a table anytime?

Good question, have you tried it out to see if it works out? Because last I checked you could always find something in a table if you had an instance variable so I mean would make sense that you could index it with everything. Only way to know for sure it to try it out.

local tabl = {1,2,"string"}

print(tabl[3]) -- > string
print(tabl["string"]) --> nil, because that index doesn't exist.
1 Like

thank you so much for all the help. this is really interesting, I had 0 clue that you can use the name of the entry in table.remove.

1 Like
local tabl = {1,2,"string"}

print(tabl[3]) -- > string
print(tabl[table.find(tabl,"string")]) --> string