Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to require "MainModule" from the client?

Recently, I found out that using Module scripts allows me to protect my work from clients till the script is complete. One problem occurred when trying to require a MainModule script from the client because it won't allow you to use the 'require()' on the client. After fiddling with RemoteEvents, I've finally ended up with a method to require a module from the client.

After solving that problem, I figured out that it will not work when running it in-game.

-- ServerScript 'A' (located in game.ServerScriptService)
local events = game.ReplicatedStorage:FindFirstChild("Events")

events.Animations.OnServerInvoke = function(player)	
	local module = require(980963185)
	return module
end
-- ModuleScript 'B' (located in game.ReplicatedStorage)
local module = game.ReplicatedStorage.Events.Animations:InvokeServer()

return module
-- LocalScript 'C' (located in game.StarterPack)
local ninmodule = require(game.ReplicatedStorage.ModuleScript)
ninmodule.Animations()

The result from running the script is as follows:

-- Players.Player1.Backpack.[S] Animations:4: attempt to call field 'Animations' (a nil value)
-- Stack Begin
-- Script 'Players.Player1.Backpack.[S] Animations', Line 4
-- Stack End

Is there any solution on how to call the module function from the client with Filtering-Enabled on?

1 comment

0

Heads up: You can't pass functions from the server to the client (and vice-versa) through Remotes. Arrays, Dictionaries, and Instances are OK, however.

1 answer

Well, I'm not quite sure on what you're asking for. But this may help:

firstly, we need to be sure that the module is wired to a mirror, so that you can call it in scripts.

ex: THIS IS THE MODULE SCRIPT

local _m = {} --mirror right here
function _m.printsomething()
	print("yo")
end
return _m

Now to link this module to a client, or a script would be done like this.

ex: THIS IS THE CLIENT, OR A SCRIPT THAT YOU ARE WIRING.

local module = require(game.ReplicatedStorage.ModuleScript)
--now to simply call the function inside the module
module.printsomething()

Simple as that.

3 comments

0

I am aware of this method, however, is there any way for the client to require a MainModule? The require() is server-only while Filtering Enabled is on.

0

Well I do believe you don't HAVE to use require but I don't really mess with modules.

0

require() isn't server-only when filtering is on...

This discussion is preserved as it was. New answers, comments, and votes are closed.