# RemoteEvent

<https://create.roblox.com/docs/reference/engine/classes/RemoteEvent>\
\
In this section, we will be discussing remote event. Remote event is an object that can be used to communicate through client to server and server to client. When you are scripting, some cases need communication between the client and server such as sending a game pass prompt when you touched a part.

{% hint style="danger" %}
There are some limits on the remote event. Please read after you finished the section by go [**here**](https://create.roblox.com/docs/scripting/events/remote#argument-limitations).
{% endhint %}

Now, let's say we got 2 scripts. One is the server script located in ServerScriptService and one is the client script (LocalScript) located in StarterGui. We also insert one remote event in ReplicatedStorage and name it MyRemote.

{% hint style="warning" %}
The remote event should be placed where the **server** and **client** can access it such as **ReplicatedStorage.**
{% endhint %}

{% hint style="danger" %}
Placing the remote event in **ServerStorage** will take the ability of the client script (LocalScript) to access it and only the server can access it since the only script that can access through **ServerStorage** is a server script, not a client script (LocalScript).
{% endhint %}

### SERVER USAGE

#### Client

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

-- Communicate the server from the client by using :FireServer() and pass in data to send to the server from the client.
myRemote:FireServer('This is a client message')
```

#### Server

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

myRemote.OnServerEvent:Connect(function(player, data)
   -- Received a message from the client and triggered the block code below.
   -- The first argument of OnServerEvent is always a local player.
   print(data) -- This will print "This is a client message"
end)
```

{% hint style="danger" %}
When sending an **instance** from client to server make sure it **exists on the server** or else the instance you send will become <mark style="color:yellow;">**nil**</mark> on the server.
{% endhint %}

### CLIENT USAGE

#### Server

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

players.PlayerAdded:Connect(function(player)
   -- Sending the message to the client when a player joins the game just for them.
   MyRemote:FireClient(player, 'This data is from the server.')
end)
```

#### Client

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

myRemote.OnClientEvent:Connect(function(serverData)
   -- The client will be triggered code block here when :FireClient() or :FireAllClients() was called.
   print(serverData) -- This will print "This data is from the server."
end)
```

### ALL CLIENTS USAGE

#### Server

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

players.PlayerAdded:Connect(function(player)
   -- Sending the message to all players (clients) when a player joins the game.
   MyRemote:FireAllClients('This data is from the server.')
end)
```

#### Client

```lua
local players = game:GetService('Players')
local replicatedStorage = game:GetService('ReplicatedStorage')

local myRemote = ReplicatedStorage:WaitForChild('MyRemote')

myRemote.OnClientEvent:Connect(function(serverData)
   -- The client will be triggered code block in here when :FireClient() or :FireAllClients() was called.
   print(serverData) -- This will print "This data is from the server."
end)
```
