A Simple Tutorial for Two Way Remote Events


  • Hello,

    Ever wondered how you can send something to the server, and expect another thing back? This tutorial for beginners will show you how (remote functions will not be discussed, and will be left for another tutorial). Firstly, add in everything that appears below:

    0_1536372672860_upload-69808177-94b9-439c-b70d-a6211706539c

    In the Workspace, add a: Baselate and SpawnLocation; in ServerScriptService: Script; in StarterGui: ScreenGui, TextButton (child under ScreenGui), and LocalScript (child under TextButton).

    Next, open the LocalScript and add:

    local clientToServer= game.ReplicatedStorage:WaitForChild("ClientToServer")
    local serverToClient= game.ReplicatedStorage:WaitForChild("ServerToClient")
    
    script.Parent.MouseButton1Click:Connect(function()
    	local msg1= "[client]: Hello Server!"
    	clientToServer:FireServer(msg1)
    end)
    
    function serverToClientFunction(msg2)
    	--do stuff here once message received from client
    		print(msg2)
    end
    serverToClient.OnClientEvent:connect(serverToClientFunction)
    

    In this instance, you are referencing the remote function that will be created from the Script, and so you wait (WaitForChild) once the game begins for it to exist. Create a button that can be used to activate the remote event from the client to the server. Next, you will need to create a function for when the server picks up the message sent from the client; the server will make a response to be sent back to the client. Think of this process as a person mailing a donation of clothes to a humanitarian organization. The organization receives the box of clothes, and after further processing, sends back a thank you note. The person receives the note, and it becomes their choice on what they want to do with the note (serverToClientFunction()).

    Then, in the Script, add:

    local clientToServer= Instance.new("RemoteEvent",game.ReplicatedStorage)
    clientToServer.Name= "ClientToServer"
    local serverToClient= Instance.new("RemoteEvent",game.ReplicatedStorage)
    serverToClient.Name= "ServerToClient"
    
    function onServerEvent(sender,msg1)
    	print(msg1)
    	local msg2= "[server]: Hello Client!"
    	--serverToClient:FireAllClients(msg2)
    	serverToClient:FireClient(sender,msg2)
    end
    clientToServer.OnServerEvent:connect(onServerEvent)
    
    

    When you create a remote event, it should be programmed in a Script. Make sure to name the remote event as well so it can be easily referenced from the local script (ex, WaitForChild("ClientToServer")). Create the function that allows the server to receive the initial message from the client, and in turn, immediately send a response back to the client upon receiving the message.

    And that is basically it! Just run the game on Studio or online and have the error log open when clicking on the button.

    Thanks,
    Houlardy


  • @Houlardy642 Parent argument to Instance.new is deprecated, RBXScriptSignal:connect() is deprecated, and global functions are bad practice.


  • Other than my scripting practices, I think it's still easier to understand than the Wiki on Remote Functions/Events.


  • @Houlardy642 Yeah but they should still be fixed. other than that the tutorial was good.


  • Ok. I searched and couldn't find a tutorial from anyone else on this in the sub-forum. Maybe someone can make one on remote functions or an additional tutorial about filtering enabled.


  • @Houlardy642 I meant the deprecated code and bad practices.


  • @Houlardy642 Why don't you want to show remote functions right now? Some people might use this as actual code.


  • @hiimgoodpack The whole point of remote functions are for two way, lol. But roblox wiki has a tutorial on this, but I still like it. OP still didn't fix the deprecated items and usage of bad practices...someone is going to take this code and learn the bad way.


  • @incapaz No, @Houlardy642 is using remote events when a remote function should be used.


  • @hiimgoodpack i meant being able to use one remote but yeah.


  • @hiimgoodpack

    So, what's the big deal?

  • Global Moderator

    @hiimgoodpack I mean you can make two RemoteEvent requests too. The behavior will be slightly different but it's perfectly fine.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.