Networking is complicated, and there are a few mistakes that people make when using RemoteFunctions. Here are the two most common.
Passing references to objects in ServerStorage
I've seen numerous developers write code like this recently:
Server script:
1 | RemoteFunction.OnServerInvoke = function (player, carName) |
2 | return game.ServerStorage:FindFirstChild(carName) |
Local script:
1 | local car = RemoteFunction:InvokeServer( "Corvette" ) |
Since the object is located in ServerStorage, it won't be replicated to the client. If you want the object to be accessible to the client, it needs to be parented to Workspace or ReplicatedStorage or another service that is accessible to clients.
Errors in RemoteFunctions are sent back to the client
If you have a RemoteFunction that performs an action that may throw an error, and you don't catch the error, it will be sent back to the client.
For example, if you have a game that uses HttpService to hit your web server, and for some reason one of the requests throws an error, the website's URL will be sent back to the client and will be visible in the Developer console.
1 | RemoteFunction.OnServerInvoke = function (player) |
Local Console:
To prevent this, you should wrap your code in a pcall (protected call) so that errors in your RemoteFunction won't be sent back to the client.
01 | RemoteFunction.OnServerInvoke = function (player) |
02 | local success, err = pcall ( function () |
10 | print ( "An error!" , err) |
Commentary
Leave a Comment