Returning data to the client

Which is better and why?

Option1:

-- LocalScript
local Event = game:GetService("ReplicatedStorage"):WaitForChild("SomeEvent")
SomeClientSideSignal:Connect(function()
    Event:FireServer(...)
end)
Event.OnClientEvent:Connect(function()
    print("Success")
end)
-- Script
local Event = Instance.new("RemoteEvent")
Event.Name = "SomeEvent"
Event.Parent = game:GetService("ReplicatedStorage")
Event.OnServerEvent:Connect(function(Player, ...)
    if SomeCondition(Player) then
        DoStuff()
        Event:FireClient(Player)
    end
end)

Option 2:

-- LocalScript
local Func = game:GetService("ReplicatedStorage"):WaitForChild("SomeFunc")
SomeClientSideSignal:Connect(function()
    if Func:InvokeServer(...) then
        print("Success")
    end
end)
-- Script
local Func = Instance.new("RemoteFunction")
Func.Name = "SomeFunc"
function Func.OnServerInvoke(function(Player, ...)
    if SomeCondition(Player) then
        DoStuff()
        return true
    end
    return false
end
Func.Parent = game:GetService("ReplicatedStorage")

I’d say Remote Functions (Option 2) work better. Even if they are out of my range, they are still better for returning values.

The second one because that’s what RemoteFunctions are designed for.

Only problem with the second sample is the way you wrote it. OnServerInvoke is meant to be defined with a function, not written as if it’s like a connection. Proper way to write it:

Func.OnServerInvoke = function(player, ...)
function Func.OnServerInvoke(player, ...)

EDIT: Corrected, thanks for pointing out. I mixed the two ways of writing them.

That’s also not the correct way to write it. The correct way to write it is:

function someFunction(player, ...)
    
end

func.OnServerInvoke = someFunction

or

func.OnServerInvoke = function(player, ...)

end

Regardless, RemoteFunctions are built to return things to the client from the server, and vice versa. It’s basically the same as sending a signal to the server from the client, then sending a signal to the client from the server. It’s just a lot more feasible to use a RemoteFunction.

1 Like
function SomeTable.SomeValue()

end

Is valid syntax that works and so is

function RemoteFunction.OnServerInvoke()

end
1 Like

Option 2 is better when you for sure get a return value.

If you don’t know when/if the server will return a value then Option 1 is better and more efficient. This is because RemoteFunctions will always return something even if it’s nil to the client, whereas RemoteEvents don’t return back to the client.

function SomeFunction()

end

Is equivalent to

SomeFunction = function()

end

And works on RemoteFunction.OnServerInvoke

There is no :Connect() so the code works and I tested code with that syntax and it worked

So I should use Option 1 if I am only telling the client whether the request succeeded or not but if any other data needs to be sent I should use Option 2?

The removeEvent create a new thread everytime it is fired. A thread is a other process running(until the function end, the thread will terminate), a thread equal to 1 megabyte of memory. So if you want to return data fire again the event you were using 2mb (one in client and other in server). And if you fire the event a lot of times you wont know what of the returned data is of each fired.

The removeFuntion only create a one thread in server and after the code return some or end, it send the data to the thread is was invoked. That thread blocks until some data is recived.
And you will know what data you need.

:man_facepalming:

I mixed the two ways into one. Thanks for correcting me on that. I should probably not write code that late at night again, lol. The two ways you wrote are functionally the same, the other way I was thinking of was, which you did not include and ended up mixing into my post:

function func.OnServerInvoke(player, ...)

cc @asadefa

What if the value is either true or nil?

I don’t know