How To Communicate between serverscripts and module scripts

Hey everyone I was wondering how We can communicate between server scripts and module scripts. Should we use RemoteFunctions, Bindables I’m Not too sure as I’m certainly not the best scripter so if anyone can tell me or if there is a tutorial out there that show how you communicate between Module scripts and server scripts I would be glad to hear, thanks For Reading!

1 Like

ModuleScripts can exist in either the server context or a client context for each client. Which one depends on which script it gets require'd from.

If your module is in a server context, then your server script can just require the module and call methods on it or whatever. That would also be true if you had a LocalScript that you wanted to communicate with a module in the same client context, just require it and use it. No communication over the internet needed.

If your module is in a different context than the script, then you’ll need to communicate somehow. If one is on a client and the other on the server, you can use RemoteEvents and RemoteFunctions (the latter with some limitations). If your want to send something from the server to all clients, you can create some instances in a place where they get replicated to clients, like creating a Part in workspace or a Value object in a player’s leaderstats.

If you want to communicate between two different clients, then you’ll need to go through the server first.

If you explain your situation a bit more I can show you some code examples

Thanks For replying so I am using AI Pathfinding The pathfinding script is a module script located in Server Storage and the server script is located in a NPC the server script within the NPC requires the AI Pathfinding module script but I’m unsure of how I can make it that when the module script detects that the NPC has finished moving that it tells the server script to carry out a specfic function I hope i explained this well enough

1 Like

You could register a callback function that you pass as a parameter to whatever function the ModuleScript has that starts the path following. A better way IMO would be to fire a custom signal or return a promise that gets resolved when the path following is done.

The callback thing can be done like this:

-- ... in the ModuleScrit
function PathModule.FollowPathAToB(A: Vector3, B: Vector3, onCompleteCallback: () -> ())
    --bla bla bla follow the path
    --once the path is completed:
    if onCompleteCallback then 
        onCompleteCallback()
    end
end

Thanks for the help I will be sure to implement this into my module cript once i get some sleep in haha I have been in studio all day and I’m exhausted thanks again!

Thanks It Works! Also one more question how do you use remote events and Bindables in Module scripts.

Bindables are like a two way script communication. I tend to use them to call and loop something that should be looped.
Remote events are local scripts to scripts, and vice versa. You can call a fire on a remote event with “:FireClient” (Server) or “:FireServer” (Local).
Bindables are fireable by writing “:Fire”

1 Like

Thanks for the feedback! Appreciate it!

Also how can I detect if a Bindable event has been fired?

I only create module scripts when they are used by server scripts, I integrate the communication code into server script. I typically don’t have communication from module <-> script because I find it confusing. Also makes me shiver because of the possible circular dependency errors.

1 Like

The wiki goes into this with code examples and everything

1 Like

If you want to communite between serverscripts and module scripts. Just do local variable = require(module script location).