How to use module scripts with Remote Events?

Hello! I am in the middle of working on a game and wanted to re-organize my scripts. I don’t know how to use the Client-Server connection between ModuleScripts and Local/Server scripts. Does anyone care to explain, give a sample of code? Or just send a youtube tutorial that has ModuleScripts being used with FilteringEnabled.

Any help is appreciated and I’d be very thankful.

3 Likes

Modules and RemoteEvents/Functions are not inherently tied or related in any way. Could you be more specific on what you mean?

1 Like

Like, how would I do

RemoteEvent.OnServerEvent:Connect(function(player,otherarguments)

in a ModuleScript? Because you need to run the function from somewhere, and I need the modules to pick up Remote requests.

Once you require the module the code should run normally.

1 Like

If a client uses a module script on their machine, it can turn out in two ways:

The module returns data for the local script to send to the server


Local Script

local Module = require(script.Module)

RemoteEvent:FireServer(Module:GetData())

Module Script

local module = {}

function module:GetData()
    return true
end

return module

Server Script

RemoteEvent.Invoked:Connect(function(Data)
    print(Data) --Prints true
end)

The local script tells the module script to send the data to the server


Local Script

local Module = require(script.Module)
Module:SendServerData(true)

Module Script

local module = {}

function module:SendServerData(Data)
    RemoteEvent:FireServer(Data)
end

return module

Server Script

RemoteEvent.Invoked:Connect(function(Data)
    print(Data) --Prints true
end)

These are all just examples and may not work, but at least you have an idea on how Remote Events and modules could communicate.

7 Likes

I personally just store functions in my modules and, on the core server script, connect those functions to events like:

--ModuleScript
local module = {};

module.test = function(plrInput)
    print(plrInput);
end

return module;
--ServerCoreScript
local module = require(script.TestModule);

someRemoteEvent.OnServerEvent:Connect(module.test);

The good side to this is, with a bit of logic code, I can use one RemoteEvent for my entire game.

I hope you find your solution!

25 Likes