Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How exactly do remote functions work?

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

3 comments

0

Code?

0

k hold on

0

Return has to be the very last thing in your code.

2 answers

✓ Accepted answer

###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)

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

1 comment

0

Yes this helped a lot thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.