I’m wondering if there’s an event that fires when you use table.insert because I want to know when an item is added to a table. Does anyone know if it exists?
Can I ask why you want to do this?
Presumably, you’re the one adding things to the table, so you already know when something new is added!
(There are legit reasons to want this, but it’s not necessarily the best solution)
Yeah, I’ve changed my mind, I’ve moved the entire thing to right after table.insert.
Instead, you can use an loop to detect changes if the last version of the table’s size is larger than the current table’s size, and do the function:
local update = {}
local table_ = {}
spawn(function()
while wait() do
update = table_
if #update < #table_ then
-- code
end
end
end)
Detecting changes in tables is never a necessity. Most solutions introduce unnecessary overhead. As @nicemike40 brilliant put, you control when things are added to a table. Instead of detecting change, run your code at the site of the change. If this is not suitable for your codebase, it’s likely the table in question bears significant value to a system, which is formally developed with an interface (APIs). That interface can run a custom event or registered callbacks at the site of change for you.
For example, in my monetization system, I have several facets for detecting the additions of game-passes and developer products from the around the codebase:
Internal site of change: