How to properly use remote functions with module scripts?

I’m trying this off with remote events but I believe im heading down the wrong path here. I’ve followed this post a bit but I still cant quite understand everything. I’m trying to get inventory data from the server, to the client using module scripts.


(client script^^)

This is in my client script, in theory, i was trying to fire to the server a function in my module script (which you can see below
RobloxStudioBeta_YfzQXQkO0Y
(module script^^)

this function works, and invData is printed. Here is where i have the question, Is it as simple as defining the returnInv event inside of this function and then calling returnInv:FireClient(invdata) back to the local script where as you can see on line 9, i’m trying to print the data fired from the module script.

This was the post that helped me the most, but i’m still a bit confused. How to use module scripts with Remote Events? - #5 by wevetments

1 Like

Firstly, you won’t be able to send anything to the server with the FireServer event as your sort function returns nothing. To fix this simply add at the end return invData. This is only a short fix though as your script is prone to exploits due to the server relying on the client for the players inventory, and if it gets spoofed they’ll be able to get any item within your game.

However, I believe your problem is that you’re using the wrong kind of Remote. Currently, you are attempting to contact the server from the client, and then the client from the server so Client → Server → Client. RemoteEvents are used for 1 way communication, however RemoteFunctions are used for 2 way. This means it’d be better to use a RemoteFunction, as you’re sending information back and forth.

So here is what i’m coming up with so far.
RobloxStudioBeta_soUV3eyYw4

its easy to setup on the client side but I’m not quite sure how to set it up from the module script on the server. I cant use OnServerInvoke on line 12

RobloxStudioBeta_QEJWThS23u

Maybe I have this setup wrong, invoke server = ping server, onserverinvoke = send data back to client? or atleast that how I thought it works.

I always had trouble with remote functions, remote events are the easier of the two to understand for me.

OnServerInvoke should only be used on the server. Whatever the server returns through the function connected to it, is what the client gets back (if the item is replicated). Vice versa with OnClientInvoke (but you shouldn’t use that).

RemoteEvents and RemoteFunctions are (almost) the same. The only difference is, you’re connecting a function to a callback instead of listening for a signal.

Example:

--client
local serverResult = RemoteFunction:InvokeServer() --asks the server for something
--server
RemoteFunction.OnServerInvoke = function()
   return 0 --sends this back to the client
end
1 Like

I’d recommend using Remote Events rather than Remote Functions as you’d have more control over how you would return the data to the client.

Essentially, you would follow this logic:

  1. Player does something to fire an event
  2. Local Script does something because of event
  3. Local Script runs code from module
  4. Module fires remote to request data from server
  5. Server script gets remote event request and returns data
  6. Local Script gets data via remote event

But using modules to fire remotes is just going make it harder to maintain your code considering you can just make it all work on 1 local script.

That makes sense.

This is how I currently have my server setup.
RobloxStudioBeta_bo4h9H4V4L

This is the client

RobloxStudioBeta_R8EuwqBpx6

However i’m getting this error

RobloxStudioBeta_Hweg36UZXf

The error means that OnServerInvoke is being connected on the client. Are you sure the script in the top picture is either a SeverScript or is a ModuleScript being required by the Server?

In the reply, the top picture is the module script and the second picture is the localscript. I believe the reason why its erroring, is because theoretically, im calling OnServerInvoke on the client (because im calling backpack:sort() in a local script).

Thats what i’m trying to figure out how to get around, I think thats what I have setup wrong.

Ok, I think I see the issue here. For the ModuleScript, don’t connect the OnServerInvoke from there, just return invData. Create a ServerScript and connect to the RemoteFunction from there. Make sure that the RemoteFunction returns a value, otherwise the client will yield forever.

Yep! That made the remote function work! However, none of my data is being passed in with it. I can see my data on the server but not the client.

RobloxStudioBeta_0pLCLOEaLy

The table below ??? is returned from

(the client)

Does this have anything to do with me calling that modulescript function in the localscript? afaik modulesripts can be required in localscripts correct?

I think that’s an issue with how Roblox replicates tables in between client → server boundaries. Tables with mixed keys (for example, strings and numbers) can cause some data to be lost.

If it’s not the replication error and Table is an Instance, make sure that it’s placed somewhere where both the client and sever can see it (like ReplicatedStorage).