Using module scripts for plugins

Roblox doesnt include “plugin” in the global library of a module script. However whenever I try to define it in an attempt to get around this, roblox flags it because its “built in globally”. See screenshot below.

I could just call the variable something else, and that would fix this problem. What I really need to know is if roblox did this for a reason. Is there a downside to using a module script when making a plugin that I am not aware of?

Thanks

Roblox just does that when you name a variable the same thing as a global keyword. It won’t throw an error, but it’ll just leave an ugly underline under it.

Wonder is it’s possible to make a plugin with module script

Just pass the plugin variable to whichever module scripts you use from the main script. That’s what I do.

Can you show an example please I saw a thread in which someone did something like this

Server script where it’s saved as local plugin

game:GetService("ServerScriptService").TimeSaverRenamer.Parent = script;
print(require(script.TimeSaverRenamer));

ModuleScript

local TimeSaverRenamer = {}

function TimeSaverRenamer:InitiatePlugin()
	print("MODULE SCRIPT PLUGIN WORK 100 %")

	
	-- Create a toolbar for the plugin
	local toolbar = plugin:CreateToolbar("TimeSaverRenamer")

	-- Create button
	local button = toolbar:CreateButton("Open TimeSaverRenamer", "Opens the Time saver Renamer", "rbxassetid://1234567")

	local isActive = false
	

	-- Button click event handler
	button.Click:Connect(function()
		isActive = not isActive
		if isActive then
			print("MODULE SCRIPT PLUGIN WORK 100 %")

			button:SetActive(true)
		else
			print("MODULE SCRIPT PLUGIN WORK 100 %")

			button:SetActive(false)
		end
	end)
end


return TimeSaverRenamer

I don’t think Roblox allow me to do this

So I succeed displaying the plugin button but all my print inside the module don’t work

I’ve done something like this:

ModuleScript:

local PluginContext = {}

function PluginContext:Initialize(plugin)
    self.plugin = plugin
end

return PluginContext

Main ServerScript:

local RunService = game:GetService("RunService")
local PluginContext = require(script.Parent.PluginContext)

if plugin then
    if not RunService:IsEdit() then
        return
    end

    PluginContext:Initialize(plugin)
    -- ...
end

Then anytime I need the plugin variable, I can just require PluginContext and use it from there

What if you want to print something when you press the plugin button

I do exactly what you’re doing. I connect a callback to the button’s Click event. Not sure why you’re not seeing your print messages

That’s weird maybe it’s because Roblox detect that I made a module script hence he definitely says it’s not a plugin script because I’m using another module but this explanation wouldn’t make sense honestly

OH IT WORKS I HAD TO RESTART STUDIO

1 Like

Out of curiosity, how are you refreshing/loading your plugin from studio?

I close Roblox Studio and open it back lol I don’t know what’s the best way to do it for plugin

I usually have a folder with all my scripts. I right click the folder and click ‘Save as Local Plugin…’. After the explorer dialog pops up, I just overwrite my previous saved plugin. Studio unloads and reloads the plugin for me.

So you don’t need to close and open back Roblox Studio each time ?

Thanks for you to make me learn this new secret appreciate I learn something new everyday

Correct. You should be able to refresh and update the plugin without having to close and open Studio each time. Glad I was able to help :slight_smile: