How to make a function run when something is added to an Array or removed

I want a function to run when something is removed or added to a table (array)
This metamethod won’t make it work so is there anyway to make it work?

local PetsMT = {
    __newindex = function()
        print("META TABLE ADDED TO")
    end,
}

local Pets = setmetatable({}, PetsMT)
table.insert(Pets, "Cat")

Expected Output: “META TABLE ADDED TO”
Output: Nothing

So I want to fire client every time Pets is added to

but the print won’t run

(I don’t know metatables that well)
Instead of using table.insert, just set the first index of the table to “Cat”

Pets[1] = "Cat"
2 Likes

it’s not gonna be the first index. the index just depends on how manyh there already are in the array
I guess Pets[#Pets + 1] = “Cat” could work?

2 Likes

then whatever index you want (30char)
yea

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.