Metatable troubles

  1. What do you want to achieve?
    Learn how to use this Metatable

  2. What is the issue?
    Metatable keeps overwriting the other metatables

  3. What solutions have you tried so far?
    I’ve looked into metatables deeper, but still can’t find anything relating to this.

I’m very new to metatables so any explanation or advice would be appreciated :smiley:

local module = {}	
module.__Index = module

--SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--VARIABLES	
module.InteractionsInGame = {	
	TeamCounter = {	
		Keybind = "E",	
		Time = 1, 		
		ActionName = "Television",
		Module = require(script.Parent.InteractionModules.TeamCounter),	
		Reload = false,	
		ReloadTime = 0
	},

	TeamCounter2 = {	 -- Added so i can see what's printing
		Keybind = "E",	
		Time = 1, 		
		ActionName = "Television",
		Module = require(script.Parent.InteractionModules.TeamCounter),	
		Reload = false,	
		ReloadTime = 0
	},
}

--MODULES
local Modules = {	
	["Core"] = {	
		ZonePlus = require(ReplicatedStorage.ReplicatedFramework.Packages._Index.ZonePlus)
	}
}

--PRIVATE VARIABLES

--PRIVATE FUNCTIONS

--PUBLIC FUNCTIONS
function module.new(InteractionName, Zone, Keybind, ActionName, Time, InteractModule, Reload, ReloadTime)	
	if InteractionName and typeof(InteractionName) == "string" and Zone and Zone:IsA("Part") and Keybind and typeof(Keybind) == "string" then		
		if ActionName and typeof(ActionName) == "string" and Time and typeof(Time) == "number" and InteractModule then	
			local self = setmetatable(module, {})
			self.Name = InteractionName
			self.Zone = Modules.Core.ZonePlus.new(Zone)
			self.ZonePart = Zone
			self.Keybind = Keybind
			self.ActionName = ActionName
			self.Time = Time
			self.InteractModule = InteractModule	
			self.ReloadTag = Reload	
			self.ReloadTagTime = ReloadTime	
				
				print(module)
			
			return self
		end
	end
end		

function module:RunInteraction(InteractModule, ZonePart)	
	InteractModule.RunInteraction(ZonePart)
end	

function module:StopInteraction(InteractModule, ZonePart)	
	InteractModule.StopInteraction(ZonePart)
end


return module 

setmetatable({}, module)

Then how would I use the functions?

Unless, you can use them either way

EDIT: Thank you man

Here’s a bit more information about metatables (in case you need it).

Metatables mean that the given table will inherit everything from the metatable it is receiving. This is why it is widely used for Object-Oriented Programming, because it means each new item of the class can call methods on itself without needing to pass things like parameters. The whole idea is that everything is local to the class.

local module = {}
module.__index = module

function module:doStuff()
    print(self.someNumber)
end

function module.new()
    local self = setmetatable({}, module) --inherits everything from "module" and returns the first parameter
    self.someNumber = 5
    self:doStuff() --outputs 5
end

(edit: __Index is not a metamethod, it should be __index).

Ahh, I wasn’t sure if it really mattered about the order I put it in

If I were to use self in a function in a metatable would it use the other tables or the exact table?

The colon notation means it passes itself as a parameter, and implicitely defines self as it.

When we made the class:

local self = setmetatable({}, module)

it inherited everything, so when we call the method

self:doStuff()

we use colon notation to pass it as a parameter. So, in short, yes, it will use the class itself if it is called from the class. Here’s a post someone made about it.

that would use the the table where ur using the metatable from

Thank you so much man! abcdefg

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.