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:
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.