I’m trying to make an inbox thing where a player’s message goes into a table called “Inbox” and I want to check for when a message is added, cause I just do table.insert(inbox, message).
If it automatically added to the table cant you just check for when the player chats?
It’s not automatically added, it first goes through a whole filter thing I made before it’s either accepted and placed into the inbox, or just ignored.
Alright. You could make it so once it added it sends that information to whatever telling that something was added
I might try __newindex but I’m not sure if it’ll work
You can use RemoteEvent, BindableEvent, ETC to achieve this if you’re trying to communicate to another script or whatever.
yeah but it’s part of a whole process so I don’t want to use a whole bunch of bindable events
I dont think theres really any preformance changes if you use alot of bindables and stuff unless you’re trying to be clean it shouldn’t be a problem
Just create a function that you call instead of table.insert.
function addToInbox(message)
table.insert(inbox, message)
-- do extra stuff
end
If you’re into it, you could use a method on an Inbox class instead.
In general, for code clarity I would recommend avoiding relying on metatable methods like __newindex
. It makes it so that you can’t trust what basic Lua operators like t[k] = v
will do, it’s a power that must be used with great care.
this is the code
function methods:AddToInbox(player, message)
local inbox = self.InboxChannel
table.insert(inbox, {player, message})
end
function methods:HandleInbox()
local inbox = self.InboxChannel
inbox.__newindex = function(self, index, value)
print("New inbox: ", index, value)
end
end
I’m trying to make it so that when “AddToInbox” is used, and inserts the thing into inbox, then inbox does what I need it to (but I haven’t added that yet)
Coudn’t you just call that function after it adds?
Oh good idea, thanks!
__newindex, but you can’t use table.insert.
local metatable = {}
function metatable:__newindex(key, value)
print("New index", key, value)
end
local list = setmetatable({}, metatable)
list[#list + 1] = "value" --You have to use list[#list + 1] = instead of table.insert since the latter doesn't call __newindex.
-- Output
-- New index 1 value