game.Players.PlayerRemoving:Connect(function(player)
local inidle = table.find(idle, player)
local inplaying = table.find(playing, player)
if inidle ~= nil then
table.remove(idle, inidle)
elseif inplaying ~= nil then
table.remove(playing, inplaying)
end
end)
Why not use metatables? This is a perfect usecase for them.
You’ll need two tables because __newindex a weird metamethod, but basically here’s the code that you would need to run code. Metamethods cannot yield.
local playing = {}
local proxy = setmetatable(playing, {
__newindex = function(self, k, v)
print("Added " .. k)
table.insert(playing, v) --we dont need to use the key here since it's an array, not a table
--run table changed code here
end})
What this setup allows you to do is check when a table updates, if you update proxy, not playing, it runs the function that __newindex is set to, we need to use a proxy table because __newindex only works when keys are added, not changed. But it does block keys from being added to the original table.