Is it possible to temporarily add an element to a table?

Basically, I’m trying to add an element to a table then remove it after a set duration of time like the Debris Service. Is this possible?

What specifically do you intend to use this for? There are a couple ways you can go about this, but actual implementation really depends on use case.

I’m just trying to add an integer to a table inside of a script, like 5. Then after X amount of seconds, remove only that value of 5 in the table, not other duplicates of 5 in the table. I’m not using this for anything in particular, in just curious as to how to do this, and if there is a correct way to do this.

Something like this can be as simple as this for dictionaries:

local tbl = {}

local function AddToDictionary(t, key, value, duration)
    t[key] = value
    task.delay(duration, function()
        t[key] = nil
    end)
end

AddToDictionary(tbl, "Key", "Value", 5)

or this for arrays:

local tbl = {}

local function AddToArray(t, value, duration)
    table.insert(t, value)
    task.delay(duration, function()
        local index = table.find(t, value)
        if not index then return end
        table.remove(t, index)
    end)
end

AddToArray(tbl, "Hi", 5)

But there are many ways to achieve this.

Edit (after fixing the 2nd method I came up with a small optimization for it found below):

local tbl = {}

local function AddToArray(t, value, duration)
    table.insert(t, value)
    local m = #t
    task.delay(duration, function()
        local l = #t -- just in case the value was removed before, we don't want to out of bounds index the array
        for i = 1, math.min(m, l) do -- basically, we know the value will be at m (or l) or before it (if a value is before it is removed, it shifts down, but won't move if a value above it is moved). Therefore we only need to search from the beginning to the length of the array at the time of insertion.
            if t[i] == value then 
                table.remove(t, i)
                break
            end
        end
    end)
end

AddToArray(tbl, "Hi", 5)

Thank you, I actually didn’t know you can use # to get the index number of the table.

It gets the length of the table, which happens to be the index of the newest item. Since it’s a number saved in a variable, if you add another value it doesn’t update (because it’s not a reference value)

Your second example won’t work. When you delete an earlier key from the table, you’ll mess up the positions of the later entries causing the wrong one to be deleted.

But the first example will work wonderfully.

So I was doing some testing, and there does seem to be an issue with this method if a value is added while another value is already in the table unless I am doing something wrong. I believe this is due to how table.remove actually shifts all index numbers in the table down by 1 if they are greater than 1, resulting in table.remove(t, index) removing nil due to this shift.

So if the table is {5, 6, 7}
and then 6 is removed before 7, the 7 won’t be removed since the function is still looking for index number 3.

Here’s what I was trying to do, but it’s probably pretty inefficient and there is probably better methods, any help on improving is appreciated.

local function tempValue(number, duration)
	table.insert(numberTable, number)
	
	task.delay(duration, function()
		local removed = false
		for i, v in pairs(numberTable) do
			if v == number and removed == false then
				removed = true
				table.remove(numberTable, i)
			end
		end
	end)
end

Thanks for bringing that up, yeah I did completely disregard that, didn’t I? Either way, I’m going to edit the original so that it works properly

@Schkr1 sorry about that, I’ve edited the AddToArray method in the answer which should work. It’s basically the same as your fix works, however it uses table.find.

What you should know, however, is that in your fix, you do loop through the entire array even if removed, I would suggest using break after removing to stop the execution of the loop and allow you to get rid of the removed bool.

2 Likes