How To Use Remote Events
What are Remote Events?
All Roblox experiences need to have a communications method between the server (where all players see whats happening) and each players client (the computer or device you are playing on). These communications are handled by events. Roblox Studio has many built in events, however you can create your own events on the server or client to communicate messages to achieve different things in your game or experience. The remote event is a one way message from the client to server or server to the client. We will just look at client to server in this example. You can however read more about remote events by clicking of the link above to visit the Roblox API.
Why would I use a remote event?
The most common use of a remote event is when the player is making a purchase of an in game item. We handle the event (clicking a button) on the players client machine, then the remote event triggers a function on the server to complete a transaction. This method reduces the attempts of exploiters to get free items or change how your experience is meant to be interacted with.
How to create a client to server remote event.
In this example we will create a button on the screen that the player clicks to buy an item if they have enough gold. If they don’t have enough we prevent the purchase and give them a warning message.
To create the example you need to create the following in Roblox Studio.
In StarterGui - Create a ScreenGui - add a text button - add a local script to the button.
In the button local script create a variable for the game service ReplicatedStorage, and the buyEvent. Add a variable for the button, then use the MouseButton1Pressed method to create a function. Inside the function add the line. buyEvent:FireServer()
In Replicated Storage - Create a remote event - name it buyEvent.
In ServerScriptService - create 2 scripts - name one mainScript, the other shopScript
In the main script create a leaderstats folder and a gold variable. Set its value to 50.
In the shop script - create a variable for the game service ReplicatedStorage, and a variable for the buyEvent. Below this create a local function called BuyEvent. Add the line buyEvent.OnServerEvent:Connect(BuyEvent) below the function.
These are the basic components of creating a remote event that will run a function on the server.
To learn move uses of remote function try one of my courses. You can purchase them for the best price by clicking on the links on my webpage. I look forward to seeing you in a course.