Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the __add metamethod not printing anything?

It's meant to print('Changed') whenever the value is changed but it doesn't work

local function createValues(playername, self)
	
	local newFolder = Instance.new('Folder')
	local kills = Instance.new('IntValue')
	local deaths = Instance.new('IntValue')	
	
	newFolder.Name = self.Properties.Name
	newFolder.Parent = metaFolder
	kills.Parent = newFolder
	deaths.Parent = newFolder
	kills.Name = 'Kills'
	deaths.Name = 'Deaths'
	
end

local meta = {}

meta.__index = meta

function meta:New(playername)
	local actualTable = {
		Stats = {
			Kills = 0,
			Deaths = 0
		},
		Properties = {
			Name = playername
		}
	}
	
	local mainTable = setmetatable(actualTable, self)
	
	mainTable.Stats.__add = function(tbl, key)
		print(key)
	end
	
	
	createValues(playername, mainTable)
end

I tweaked the script a bit but it still doesn't seem to work. There are no errors and when I add to the metatable I'm doing this

childMeta[player].Stats.Kills += 3

2 comments

0

Can you elaborate on the script you gave above? Like, what does 'self' contain and what errors did you get? Also, I believe mainTable.Stats alone is not yet a metatable, so the function wouldn't work (assuming you're trying to do 'mainTable.Stats + playername' in that createValues function)

1

Okay I edited it

1 answer

✓ Accepted answer

__add will not fire for childMeta[player].Stats.Kills += 3

it fires if you do tableWithMT + something

example

local tbl = setmetatable({},{
	__add = function(t, v) 
		print(t, v)
		return true
	end
})

print(tbl + "anything") -- prints true, the return defines what he + operator will do on that table


what you probably want in the example you gave is __newindex metamethod it fires when you try to set the value of an index that doesnt exists but in your example the value already exists so what we do is we create the actual table behind the screen and give them an actually empty table with an MT that does the interaction with that table so that it seems like a normal table

example

local actualTable = {
	Stats = {
            Kills = 0,
            Deaths = 0
        },
    Properties = {
        Name = "playername"
    }
}

local tbl = setmetatable({},{
	__newindex = function(t, k, v) -- fires only when you set a value to an index that doestn exist
		print("changed")
		actualTable[k] = v
	end,
	__index = function(t, k) -- fires only if you try to get the value to an index that doesnt exist
		print("retrieve value")
		return actualTable[k]
	end
})

print(tbl.newKey) -- prints retieve value and nil
tbl["newKey"] = "a new Value" -- prints changed
print(tbl["newKey"]) -- prints retieve value and "a new Value" 

tbl.Stats.Kills = 1 -- doesnt fire newindex because stats doesnt have a metatable so you have to create one for this too

3 comments

0

This is a module script tho... How am I able to access the actualTable since I make a new metatable whenever somebody joins

1

bruh keep a dict of actualTables with the player as key,

1

modulescript makes zero difference

This discussion is preserved as it was. New answers, comments, and votes are closed.