How Do I return a value from a remote event?

So I want to make a remote event that returns a value from a localscript, heres what I have so far

Client

game.ReplicatedStorage.GET_NOTEBOOKNUMBER.OnClientEvent:Connect(function()

return Notebooks

end)

Server

function IfDoorOpen(plar)
	game.ReplicatedStorage.API_GET_NOTEBOOKNUMBER:FireClient(plar)
end

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	print(IfDoorOpen(plr))

What happends It should return is a 0 as that is the variable but instead it returns a blank string
image Can someone explain how this is not working thanks!

4 Likes

How Do I return a value from a remote event?

You don’t - can’t return through remote events, use RemoteFunctions instead for this purpose.

22 Likes

Like @XxELECTROFUSIONxX said, you can’t use RemoteEvents to return information. To do that, you’ll need to use RemoteFunctions. Example:

local remoteFunction = -- path to RemoteFunction
remoteFunction.OnServerInvoke = function(player)
    return YourInformation
end

To return information from the client to the server, use:

remoteFunction.OnClientInvoke = function()
    return YourInformation
end
16 Likes

Where does “YourInformation” come from?

“YourInformation” is just the data you want to return

Let’s say I want to print YourInformation in the client’s side. What would I do to do that?

You mean like this?

-- Server
RemoteFunction.OnServerInvoke = function(player)
    return "Literally anything you want here"
end
-- Client
local returnedValue = RemoteFunction:InvokeServer()

print(returnedValue) -- "Literally anything you want here"

You can even pass multiple values as varargs. So return x, y, z local x, y, z = RemoteFunction:InvokeServer()

4 Likes