hi, I have a question is a way to detect if:
local tbl = {'thingy'}
table.remove(tbl,1)
is a way so an Event is fired and get the removed Index?
hi, I have a question is a way to detect if:
local tbl = {'thingy'}
table.remove(tbl,1)
is a way so an Event is fired and get the removed Index?
There isn’t an event, remember that only built-in objects added by roblox have events, but you can create a while loop that constanly (every 2 seconds or so) checks wether it was removed or not.
while true do
wait(2)
if not(tbl[1]) then
--do something
end
end
If thingy
was removed from the table, then tbl[1]
would become nil, and doing not(tbl[1])
, or in other words not(nil)
should turn it true thus the if statment continues.
Or if you wanna go even crazier, use the magic of metatables, which is way more efficient.
local tbl = {"thingy"}
setmetatable(tbl, {__index = {function(tbl, index) 'code here' end)})
table.remove(tbl, 1)
local a = tbl[1] --and as soon as tbl[1] is mentioned, the code inside the function above should run
Not explaining what’s going on honestly, so here is a small explanation
@briana_superstar If you wanted an event to fire after the index would remove, I suggest using BindableEvents, which work similar to RemoteEvents except they’re for server-server or client-client communication, rather than server to client. What you can do, is at the end of the table.remove
, you can add a BindableEvent:Fire(INDEXHERE)
, which would fire that index across.
Alternatively, if you only want to find out whether an index has a value or not, all you have to do is a simple if statement:
if tbl[1] == nil then
The reason why I wouldn’t use a not (tbl[1])
is because what if the value is boolean
. You may want a table value that has either a true
or false
value, and having a not statement can potentially cause issues as a false
would be mistakenly thought of as a nil
.
I hope my side helped!
Instead of actually checking if the index was emptied right after removal, just run lines after table.remove()
.
local tbl = {"Object"}
table.remove(tbl, 1)
-- code here
Context matters, depends on the intended code logic. If you have a table with completely randomly generated contents and you want to trace indexes, try the alternative method with dictionaries or fixed numeral table.