Scripting Helpers is winding down operations and is now read-only. More info→
← Blog Home

Common mistakes with RemoteFunctions

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:

RemoteFunction.OnServerInvoke = function(player, carName)
    return game.ServerStorage:FindFirstChild(carName)
end

Local script:

local car = RemoteFunction:InvokeServer("Corvette")
print(car) --> nil

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.

RemoteFunction.OnServerInvoke = function(player)
    --
    local contents = game:GetService("HttpService"):GetAsync("http://www.this-site-has-an-error.com/")
    return contents
end

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.

RemoteFunction.OnServerInvoke = function(player)
    local success, err = pcall(function()
        local contents = game:GetService("HttpService"):GetAsync("http://www.this-site-has-an-error.com/")
        return contents
    end)

    -- `success` is a bool (true/false) that is false when the function in the pcall has errored
    if not success then
        -- If there is an error, a second value is returned, the error that occurred
        print("An error!", err)
        -- We also return here, so the function stops here and doesn't continue on
        return
    end

    -- If `success` is true, then any values it returns will be passed after the `success` bool
    return err -- Note, this isn't actually an error, but instead `contents`
end
Posted in Scripting Tips

Commentary

Leave a Comment

Diitto says: January 18, 2015
On the RemoteFunction redirection error, shouldn't the script have a check to see if the protected call was successful, and if so, return the second argument?
JasonTheOwner says: March 13, 2015
Even after parenting something to replicated storage,im finding that an item such as a tool isn't being replicated, if parented from a localscript. It shows up on the explorer of the client, but not the server. So even if I want to just pass a name reference to a server event to get the newly parented object, I'm stuck with nothing on the server. Its frustrating.
User#2 says: April 5, 2015
@Diitto Edited the post to fix that!