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.
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
(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.
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.
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
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
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.
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).