Using a table inside a function in the module script

Hello.

While i’m updating my module called XRNotifications i wanted to make a table with some variables inside the function Notify() so you don’t have to add optional variables but i’ve ran to the problem because i got an error that says:

attempt to call a nil value

while using the Notify() function.

I don’t know how to use the table inside the function properly and i want to make it because i was inspired by the UiShelf module script but while i was trying to understand the script my mind was broken and i understanded only a few things on the script.

Anyway here’s the script:

local XRN = {}

local TweenService = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local NotificationList = PlayerGui:WaitForChild("XRNotificationsGui").Main:WaitForChild("NotificationList")

local NOTIFICATION_COOLDOWN = script:GetAttribute("Cooldown")
local NOTIFICATION_TWEEN_TIME = script:GetAttribute("TweenTime")

type XRNT = {
	Title:string,
	Description:string
}

function XRN.Notify(Title:string, Description:string)
	local self = setmetatable({}, XRN)
	local NewNotifyUI = script:WaitForChild("MainNotification"):Clone()

	local Notification = NewNotifyUI.Notification

	local NotifySound = Instance.new("Sound")
	
	NewNotifyUI.Notification.Title.Text = Title
	NewNotifyUI.Notification.TextLabel.Text = Description
end

return XRN

If someone can fix it thank you.

Which line of code is erroring? As things are I don’t see anything potentially producing that particular error from within the Notify function. The things you are calling are setmetatable, :WaitForChild, :Clone and Instance.new, all of which are spelled correctly as far as I can see.

If you have omitted some of the function’s code for whatever reason, you shouldn’t do that, it would make helping out much more difficult. I say that because I see the possibility of you doing return self at the end of the function, and you haven’t set XRN.__index anywhere, so in that case if you later try to call .Notify on that returned value (the self), you would get that error, because .Notify isn’t pointing to anything, either directly or via __index.

Also, you might want to rethink using metatables in this case. I get the idea behind it, it’s fine but unnecessary and improperly executed. You could instead just do something like:

type extras = { titleColor: Color3?, descriptionColor: Color3? } -- etc

function XRN.Notify(title: string, description: string, extras: extras?)
    -- do necessary stuff
    if extras then
        -- go through the additional parameters and apply them
    end
end

And then use the “extras” table (call it whatever you like) for any additional things you might want to change.

There might be other valid reasons why you want to use metatables, I’m not saying it’s unnecessary for sure, but if all you want to do is what you’re doing in that snippet then it is for sure.

I’ve just figured out how to fix the error and i’ve used your method wich it was easier to use.

Thank you for that.