How do I fix nothing happening with this modulescript and the metatables inside it?

Brace yourself, you’re about to see some terrible coding that may scar you for life.

**Backstory: this is the tragic programming failure that came from the messy depths of my notification module. what this is meant to do is allow the user to create a new notification object and be able to show it by using :notify()

local module = {}
local mt = {__index = script.corner:Clone()} -- this clones the notification object
local Class = setmetatable(module, mt) -- I set the metatable of the module to mt

function module.new() 
	Class.__index = script.corner:Clone() -- create a new notification object
	return module --[[
        so you can be like:

        local notification = module.new()
        notification:notify()

        ]]
end
function module:notify()
	getmetatable(module).__index.Visible = true -- get the newly created metatable and show the notification object
	return
end

in case you couldn’t already tell, I have no clue what I’m doing

this is embarrassing that I have to post about this but I desperately need answers.

thx :smiley:

1 Like

You could do something like this:

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui

local notification = {}
notification.__index = notification

-- Creates the notification object
function notification.new()
	local self = setmetatable({}, notification)
	
	-- notification gui reference
	self.notificationGui = script.NotificationGui:Clone()
	
	return self
end

-- Method that is used to show the notification gui
function notification:notify()
	-- Parent the notification gui
	self.notificationGui.Parent = PlayerGui
end

return notification

Example usage:

local Notification = require(script.Parent:WaitForChild("Notification"))

local notificationObject = Notification.new()
notificationObject:notify()

image

1 Like

__index can (or should) only accept a table or a function. Instances are userdata. Can’t use index like this, but you can write a proper OOP class and set your notification UI as a property of the class object, as demonstrated in the above post.

2 Likes

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