Can't find reason for error "attempt to index nil with CreateToolbar"

Hi,
can someone help me with this?

local module = {enabled = false}
module.__index = module

function module:CreateToolBar(toolbarName)
	local newToolbar = setmetatable({}, module)
	
	local Toolbar = plugin:CreateToolbar(toolbarName)
	newToolbar.Toolbar = Toolbar
	
	return newToolbar
end

That´t the error:

19:16:19.131 - user_ModuleTest.rbxmx.PluginModuleTest.PluginCreator:7: attempt to index nil with 'CreateToolbar'
19:16:19.132 - Stack Begin
19:16:19.132 - Script 'user_ModuleTest.rbxmx.PluginModuleTest.PluginCreator', Line 7 - function CreateToolBar
19:16:19.132 - Script 'user_ModuleTest.rbxmx.PluginModuleTest.Script', Line 3
19:16:19.132 - Stack End
Full ModuleScript
local module = {enabled = false}
module.__index = module

function module:CreateToolBar(toolbarName)
	local newToolbar = setmetatable({}, module)
	
	local Toolbar = plugin:CreateToolbar(toolbarName)
	newToolbar.Toolbar = Toolbar
	
	return newToolbar
end

--== AddButton ==--
function module:AddButton(name, tooltip, iconId, UI)
	local isopen = false
	local button = self.Toolbar:CreateButton(name, tooltip, iconId)
	local guiSettings
	
	--== Loop Through Children ==--
	for i, mainUI in pairs(UI:GetChildren()) do
		--== Error if too many children ==--
		if #UI:GetChildren() > 1 then
			error("PluginCreator: Only 1 Frame in UI! The Frame in the UI will give the <DockWidgetPLuginGui> the Main Size!")
		else
			--== Create DockWidgetPluginUi ==--
			guiSettings = DockWidgetPluginGuiInfo.new(
				Enum.InitialDockState.Left,
				module.enabled,   -- Widget will be initially enabled
				true,  -- Don't override the previous enabled state
				mainUI.Size.X,    -- Default width of the floating window
				mainUI.Size.Y,    -- Default height of the floating window
				50,    -- Minimum width of the floating window (optional)
				50     -- Minimum height of the floating window (optional)
			)
		end
		
		local interface = plugin:CreateDockWidgetPluginGui(
			name,	 guiSettings
		)
		
		UI.Parent = interface
		button.Click:Connect(function()
			isopen = not isopen
			
			module.enabled = isopen
			button:SetActive(isopen)
			
			return UI
		end)
	end
end

return module

I’m trying to create a module to easily create a Plugin easily. Module is with Advanced OOP.
Hope someone can help me!
Thanks :smiley:

1 Like

It sounds like function module:CreateToolBar(toolbarName) is not receiving anything for the toolbarName variable.
Have you tried doing the obvious of a print statement straight after that line to make sure it is being passed something?

function module:CreateToolBar(toolbarName)
	print(toolbarName)
	...

Is createToolbar separate from createToolBar or is it misspelled? If it’s not separate then that could be your issue, otherwise I’m not sure.

Also, what is plugin supposed to be? The fact that it says that it is trying to index your createToolbar function with a nil value means that there is something wrong with that variable.

ModuleScripts aren’t sandboxed in the plugin environment so the plugin global is undefined. Only regular scripts are injected with the variables of and run in a plugin environment. You will need to add a function that passes the object that the plugin variable references in order to use it with ModuleScripts.

Your module:

local plugin

local module = {enabled = false}
module.__index = module

function module:CreateToolbar(toolbarName)
    local newToolbar = plugin:CreateToolbar(toolbarName)

    local toolbar = setmetatable({
        Toolbar = newToolbar
    }, module)

    return toolbar
end

function module:Init(pluginObject)
    plugin = pluginObject
end

-- Your other stuff here

return module

And then your script:

local foobar = require(moduleHere)
foobar:Init(plugin)
foobar:DoYourOtherStuff()
3 Likes