Can you return a value using a remote function?

Hey guys, its 2am right now so it will be a bit before I start to respond

Not sure if you need my code or not, but I am just wondering if you can send values through a remote function like you can a remote event?

Forgive any spelling errors I make, but will this work?

function example()
local exampleval = script.Parent.example.Value
return exampleval
end

then on the client:

--what do I put here to accept the exampleval?

Thanks for your help, I am going to sleep now

Assuming I understood your question right, no, you cannot send a function over a remote function or event. Refer to the “non-string indices” section of this article. Bindable Events and Functions | Roblox Creator Documentation

'morning

As the Developer Hub mentions on RemoteFunctions documentation, at Code Samples, it clearly shows how to do that.

Thought he meant a Value…? Haven’t I read right?

2 Likes

It’s definitely not 5 AM here

It’s possible to return back a value using a RemoteFunction, you just have to figure out what exactly you want the client & server to detect :thinking:

Firstly, you should already have a RemoteFunction inside the ReplicatedStorage Service so that it can replicate across both sides of the game itself (Server/Client)

--Server
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")

local function Example()
    
end
--Client
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")

From this instance, it looks like you’re wanting to Invoke the Server it looks like

If I recall correctly from what these RemoteFunctions do, they can return 1 value when called by the opposite side of the game, you just need to call their functions properly (InvokeServer/OnServerInvoke)

To make a request from the client/server side, call the InvokeServer function like so:

--Client
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")

local PassedValue = RemoteFunction:InvokeServer() --This will send a request from the client, to the server & will wait/yield until you get a result

if PassedValue then
    print("This is the value that passed: "..PassedValue)
end

And to check the RemoteFunction whenever you make a request on the client, you’ll have to use OnServerInvoke in order to connect these said “requests” so that the script know when to detect it:

--Server
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")

local function Example(Player) --RemoteFunctions/RemoteEvents already pass the player as the first parameter when used by the server
    local ExampleVal = script.Parent.example.Value

    return ExampleVal
end

RemoteFunction.OnServerInvoke = Example --Here, we're connecting our RemoteFunction to detect the changes

And if everything should work right, it should properly print out the ExampleVal on the client!

8 Likes

Like a value, for example
you have a value thats 0, can you tell the client that that value is zero?

Thanks for your continuous help. It seems like the client is not getting the value. I set up something:

--client
local RemoteFunction = game.ReplicatedStorage:WaitForChild("requestnew")

local PassedValue = RemoteFunction:InvokeServer() 

script.Parent.MouseButton1Click:Connect(function(thisisgettingannoying)
	if PassedValue then
		print("This is the value that passed: "..PassedValue)
	else
		print("No value")
	end
end)

When I click the button, it just says “No value”

--server
local RemoteFunction = game.ReplicatedStorage:WaitForChild("requestnew")

local function Example(player) parameter when used by the server
	local ExampleVal = script.Parent.example.Value
	print(player.Name, ExampleVal)
	return ExampleVal
end

RemoteFunction.OnServerInvoke = Example

Yea, you read that right. Like a bool value to be more specific

Ive tried that, it does not work, don’t know why. No output errors

1 Like

I think the problem may be that you are calling the function when the script first runs, not when the button is clicked.

Change it to

local RemoteFunction = game.ReplicatedStorage:WaitForChild("requestnew")

script.Parent.MouseButton1Click:Connect(function(thisisgettingannoying)
    local PassedValue = RemoteFunction:InvokeServer() 
	if PassedValue then
		print("This is the value that passed: "..PassedValue)
	else
		print("No value")
	end
end)

It should be Ok then

1 Like