How do you make stuff in array/dictionary dissapear after a number of seconds

Hello,
I would like to be able to make it so that everything added inside a table dissapears after a number of seconds
Like, I have this

local tabal = {
guran = 0
gurkhan = 1
}

then it wait 5 second,
then BOOM! All of it dissapear
then I want anything insert into the table to dissapear after 5 second to like

tabal["gurukhai"] = 2

then wait 5 seconds…
then…OOM!
then it dissapear from table
How I can do this?
- Br, iSyriux

You could have a function do this for you automatically for a given index to create.

local function AddTempIndex(tab, index, value)
tab[index] = value --//Set new index in specified table

delay(5, function()
tab[index] = nil --//Remove index from table after 5 seconds
end)
end
2 Likes

This is simple…

local myTable = {}
while wait(5) do
    for i,v in pairs(myTable) do
        table.remove(myTable,i)
    end
end

Hello,
No this is not simple, I dont want to remove all the stuff in a table at every 5 seconds because I want to make it so that each stuff is removed at 5 seconds relative to when the joined the table

Hello,
Could you explain what AddTempIndex is? Coul help

So you want it that stuffs that were added inside the table just now and make it removed?

Oh also, this not arrray, Im try to do dictionary too

It’s just the name of the function.

So both, and you want to add a temporary element inside a table and then remove it after a certain amount of seconds?

I called it that because indexes it creates in the specified table will only be existent in memory for 5 seconds.

e.g:

local fruits = {}

AddTempIndex(fruits, "Apple", 5) --//table, key, then value

Hello,
Also dictionary I want this to works with dictionary

Hdello,
Well i never heard that be use before

It’s just the function name/identifier, it can be anything you want.

Wait so it a function? AddTempIndex a functionLikethis?

local function AddTempIndex(a,b,c)
a[b] = c
wait(5)
a[b] = nil
end

Wait so if i uses a function then it will wait so do I gotta use a coroutine?

Hello,
Plz help? I wanna to make this able to run any time and dont wait for the function to end to do another function

Yes.
If you call this function, then it will set a value, pause the function that called it for 5 seconds, then delete the value.
You will need a delay() or a coroutine to make it set the value and immediately continue without waiting for the value to expire.

local function AddTempIndex(a,b,c)
a[b] = c
delay(5, function() -- added
a[b] = nil
end)
end
1 Like

Hello,
I’ve never heard of delay() is it like the same as a coroutine? Should I use it instead of wait()?

delay(n, fn) will make a function run after n seconds. It uses wait() internally. It is pretty much the same as:

function delay(n, func)
	(coroutine.wrap(function() -- use a coroutine so that wait() pauses the coroutine, not the current thread
		wait(n) -- will not affect the user of delay()
		func() -- call the original function
	end))()
end

print("This runs first")
delay(1, function() print("This runs third!") end)
print("This runs second")
wait(2)
print("This runs fourth")

edit: this function runs exactly the same way delay() does and you don’t need to include it in your script

2 Likes
local function AddTempIndex(a,b,c)
	a[b] = c
	delay(5, function() -- added
		a[b] = nil
	end)
end

Note that this function has a few bugs (or rather, it works exactly as written, nothing more or less, but you might not expect it to work the way it does)

Firstly, if you set a value, and set it again before it expires, then the value will become nil exactly 5 seconds after it was first set, but less than 5 seconds after it was set the second time.

local tbl = {}
AddTempIndex(tbl, "test", "hello")
print(tbl.test) -- hello
wait(2)
AddTempIndex(tbl, "test", "world")
print(tbl.test) -- world
wait(4)
print(tbl.test) -- nil
-- (test = world) expired in less than 5 seconds - in fact, it expired in 3 seconds

This bug can be fixed by never setting a key twice within 5 seconds :slight_smile:
If you need to do that, then the code can be changed to make sure it’s not deleting what it didn’t set.

local function AddTempIndex(a,b,c)
	a[b] = c
	delay(5, function()
		if a[b] == c then -- nothing else has changed this key in these 5 seconds, it has remained c
			a[b] = nil
		end -- else don't delete it, someone else set it after me
	end)
end

local tbl = {}
AddTempIndex(tbl, "test", "hello")
wait(2)
AddTempIndex(tbl, "test", "world")
wait(4)
print(tbl.test) -- world

The bug remains if you try to set the same key to the same value twice within 5 seconds.
Either just don’t do that, or keep a tally of how many times each has been changed (even if it is changed to the same thing), and don’t nil it if (tally at the time the key was set) ~= (tally after 5 seconds)


Another “bug” is that it isn’t aware of arrays.

local tbl = {}
AddTempIndex(tbl, 1, "hello")
wait(1)
AddTempIndex(tbl, 2, "france,")
wait(1)
AddTempIndex(tbl, 3, "brazil,")
wait(1)
AddTempIndex(tbl, 4, "india")

print("Length of table:", #tbl)
print("Testing 1")
for i,v in ipairs(tbl) do print(i, v) end -- 1hello 2france, 3brazil, 4india

wait(2.5)

print("Length of table:", #tbl)
print("Testing 2 (ipairs)")
for i,v in ipairs(tbl) do print(i, v) end -- WHAT! NOTHING?
print("Testing 3 (pairs)")
for _,v in pairs(tbl) do print(i, v) end -- 2france, 3brazil, 4india

The reason is that an array is the part of the table whose keys are numbers 1, 2, 3 etc. starting from 1, and which does not include nil.
tbl[1] would be nil at that time, so that is where the array ends, even if there is more after that nil.
The cause: that function does not shift the values back down like table.remove does, so gaps remain in the table.
Just adding table.remove to the function (replace a[b] = nil with table.remove(a, c, b)) will make it much, much buggier and worse. There is much more to it, and it can’t be programmed in one evening.


Using a metatable, it is possible to make a table use that function, without having to call it.

local tbl = CreateTempTable() -- make a special table
tbl.hello = "world!"
print(tbl.hello) -- world!
wait(6)
print(tbl.hello) -- nil

-- implementation

function AddTempIndex(a,b,c)
	rawset(a, b, c) -- changed to avoid an infinite loop and crash
	delay(5, function()
		-- if a[b] == c then -- removed this, because keeping them in will introduce ANOTHER bug (__newindex does not run if index was not nil before, it runs on *new* index)
			rawset(a, b, nil)
		-- end
	end)
end

local mt = {
	__newindex = AddTempIndex -- literally that same function. it will be run any time a new value is created in a table whose metatable is this (mt)
}

function CreateTempTable()
	return setmetatable({}, mt) -- make a new table and make its metatable the above one
end

I have not tested this code