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

How exactly do remote functions work?

Asked by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

I know how to use remote events but not remote functions. I made a Local Script that makes it so that when i press a button, i use :InvokeServer() and on the Server Script that's receiving the invoke, a part is created. I tried to return the part so that my Local Script has access to it but i can't seem to do it.

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char.Humanoid

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.G then
        RemoteFunction:InvokeServer()
    end
end)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

function RemoteFunction.OnServerInvoke(player)
    return local Box = Instance.new("Part",game.Workspace)
    Box.Transparency = 0.5
    Box.CanCollide = false
    Box.Anchored = false
    Box.Size = Vector3.new(19.29, 0.87, 2)
    Box.Name = "Box"
end

0
Code? thebayou 441 — 5y
0
k hold on Sorukan 240 — 5y
0
Return has to be the very last thing in your code. User#19524 175 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

A RemoteFunction is similar to a RemoteEvent in that it helps make your game FE compatible. They are used for things such as checking the price of something (if your game has buyable items). They can also return, something RemoteEvents cannot do. Take this for example:

--Server script

local remoteFunction = game.Workspace.RemoteFunction

local CreatePart

CreatePart = function(player)
    print(player.Name, "wants to create a part")
    local newPart = Instance.new("Part")
    newPart.BrickColor = BrickColor.new("Really red")
    newPart.Transparency = .5
    newPart.Material = Enum.Material.Neon
    newPart.Name = player.Name.."'s Part"
    newPart.Parent = game.Workspace
    return newPart
    -- Returns the part, makes InvokeServer act like the part. Required. 
end

remoteFunction.OnServerInvoke = CreatePart
--OnServerInvoke is a callback, not an event. it's a common mistake to use OnServerInvoke:Connect() 

From your local script:

local newPart = game.Workspace.RemoteFunction:InvokeServer()

print("The server made a new part for me:", newPart)
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Remote functions have different syntax compared to remote functions. OnServerInvoke is not called as an event, but with an assignment operator, since Remote Functions return a specific value back to the client. A framework for your code would look like so:

--Localscript
UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.G then
        local returnValue = RemoteFunction:InvokeServer() --InvokeServer will return something, which is then stored in the returnValue variable
    end
end)

--Serverscript
function doSomething(player)
    local Box = Instance.new("Part",game.Workspace)
    Box.Transparency = 0.5
    Box.CanCollide = false
    Box.Anchored = false
    Box.Size = Vector3.new(19.29, 0.87, 2)
    Box.Name = "Box"
    return Box;
end

RemoteFunction.OnServerInvoke = doSomething --Use the assignment operator to bind the function

Hope that helped

0
Yes this helped a lot thanks! Sorukan 240 — 5y

Answer this question