I was wondering if there is a metamethod or some other thing that will allow me to see if a new value was added to a table?
--[[Purpose:I'm making a script that is finding npcs with the value "Shop"
and making them into a shop]]--
NPCs={}--Table where the npc's will be stored
function FindNpc()--Will Search Workspace for a humanoid if it finds it then it search for the value "Shop" then insert the NPC to the Table
for Index,Objects in pairs(game.Workspace:GetChildren()) do
local FoundHumanoid = Objects:FindFirstChild("Humanoid")
if FoundHumanoid then
local FoundShopValue = FoundHumanoid:FindFirstChild("Value")
if FoundShopValue then
if FoundShopValue.Value == "Shop" then
table.insert(NPCs,FoundHumanoid.Parent)
end
end
end
end
end
FindNpc()
--To be Finished
Edited:
--[[Purpose:I'm making a script that is finding npcs with the value "Shop"
and making them into a shop]]--
NPCs={}--Table where the npc's will be stored
Metatable = {
__newindex = function(self,index,value)
print(value)
end
}
setmetatable(NPCs,Metatable)
function FindNpc()--Will Search Workspace for a humanoid if it finds it then it search for the value "Shop" then insert the NPC to the Table
for Index,Objects in pairs(game.Workspace:GetChildren()) do
local FoundHumanoid = Objects:FindFirstChild("Humanoid")
if FoundHumanoid then
local FoundShopValue = FoundHumanoid:FindFirstChild("Value")
if FoundShopValue then
if FoundShopValue.Value == "Shop" then
table.insert(NPCs,FoundHumanoid.Parent)
NPCs.x = "a"-- Y U NO print the value
end
end
end
end
end
FindNpc()
--print(NPCs[1])
--To be Finished
If there is a way to see if a value was added to a table if so what is it?

4 comments
Sorry to ask this but I just learned how to use metatables and I was wondering why did you put "t.x = "a" if there is no x in the table t?
That isn't a metatable question as much as it is a table question. A singular period symbol is what we call "syntactical sugar" for square brackets (so t.x = a is the same as t["x"] = a). By accessing this index which has nothing yet in t and then setting it equal to some value, I trigger the __newindex metamethod.
Thanks, and I put the metatable part that you showed me and it won't print.Why?
__newindex has two underscores, not one. All metamethods use two underscores.