Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What are RemoteFunctions useful?

Asked by 4 years ago

I know about RemoteEvents, but what are RemoteFunctions for? I still dont know what for.

0
The response below is amazing so definitely read it. The basic difference is that a remote event is fired and thats the end of that. A remote function on the other hand sends a request and waits for a response. Alexander_Ashford 231 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

RemoteFunctions

A RemoteFunction allows for client <-> server communication kinda like RemoteEvents. The difference is that you do not fire any events, you assign OnServerInvoke to a function, think of this like a property.

The function can return values, and the calling side will yield (or pause) until a result is received.

There cannot be multiple OnServerInvokes either.

Example code on the server.

```lua local rf = Instance.new("RemoteFunction"); rf.Name = "testRF"; rf.Parent = game:GetService("ReplicatedStorage"); local items = game:GetService("ServerStorage").items; --// some folder with weapons

function rf.OnServerInvoke(client, weaponName) --// client is the player that invoked return items[weaponName].price.Value; --// tools have a numberValue named price whose value is the price of the weapon end; ```

Example code on the client.

```lua local result = game:GetService("ReplicatedStorage"):WaitForChild("testRF"):InvokeServer("AK-47"); print(result); -- output;

--[[ 450 ]]-- -- random price idk

```

On the client, you OnClientInvoke, though it is not recommended and you actually rarely need to use this, it is not recommended because it is possible for an exploiter to return bad information or not return at all, the latter being worse since the server will yield indefinitely, waiting for a response. Theoretically you can invoke in a separate thread via coroutines.

Example code on the client. [2]

```lua local client = game:GetService("Players").LocalPlayer;

function rf.OnClientInvoke() --// no player here return client:GetMouse().Hit; --// server probably needs the mouse hit end ```

Example code on the server. [2]

lua game:GetService("Players").PlayerAdded:Connect(function(client) wait(4); local hit = rf:InvokeClient(client); print(hit); end);

It is important that you pass the player you want to invoke. Or else Roblox won't know who to work with. Player is always the first argument when calling :InvokeClient, and then anything else afterwards

```lua rf:InvokeClient(client, ...) rf.OnClientInvoke(...) -- notice no player, it's a mistake to add it here

```

---

Answer

Generally you would use remote functions client -> server when the client needs to get something only the server can access. Like maybe you have some stats stored on the server and the client needs them. You would rarely use server -> client, but a usecase would be the other way around, the server requesting something only the client can see, like the mouse target for example.

Ad

Answer this question