Help with my metatable [make script listen for mathematical operations?]

Don’t really know how to summarize my problem in the topic name, so here we are.

Let’s say I make metatable:

local Arena = {}
Arena.__index = Arena

and after that I set a metatable to a player:

local function AddProfile(Player)
	if Player then
		local NewProfile = {
			Weapon = {
				[1] = {
					Name = "Glock";
					Ammo = 120;
				};
				
				[2] = {
					Name = "Hand";
					Ammo = 0;
				};
			};
		}
		setmetatable(NewProfile, Arena)
		
		Arena.Profiles[Player] = NewProfile
	end
end

This profile is used across different scripts.

So what I want to achieve is, how do I make the script listen to when mathematical operations are used in the profile, and run a function. I don’t seem to see a metamethod that does this…

Basically an event that runs when you add/subtract ammo in one of the weapons. Any help is appreciated!

You have to use the mathematical metamethods:

  • __add
  • __sub

If you plan on using metamethods to run down the ammunition. Or you can just set a function to the object itself to handle the math.

2 Likes

It took quite a long time to learn the metamethods, but I got it. Thanks!