RemoteEvent

RemoteEvent : Instance

Overview

RemoteEvent is a class that provides a one-way communication method between a server and a client and a client-client.

Description

RemoteEvent is an object that enables asynchronous one-way communication between clients and servers, which allows clients and servers to exchange data with each other and is used to implement various interactions in the game.

Key Features

  • Asynchronous communication: RemoteEvent runs the following code immediately without waiting for a response when sending and receiving data. This is advantageous for minimizing network delays and keeping the flow of the game smooth.

  • One-way communication: You can transfer data from clients to servers, from servers to specific clients, or from servers to all clients.

  • Location: Remote Event objects are typically stored in Replicated Storage, allowing access to both the client and the server.

Properties

Methods

FireServer

The FireServer method executes server-side functionality within the context of Lua or initiates communication with a server for data transfer.

Parameters

Tuple Arguments

Return

void

Code Samples

-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")

local SomeNumber = 10
C2S_EnterPlayer:FireServer(SomeNumber) -- Passing arguments


-----------------------------------------------------------------------------------
-- Script
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")

local function OnEnterPlayer(player, number)
    print("[OnEnterPlayer]", player.Name, " / Parameter : ", number)
end
C2S_EnterPlayer.OnServerEvent:Connect(OnEnterPlayer)

FireClient

Fire an event to the specified player client.

Parameters

Player Player

Tuple Arguments

Return

void

Code Samples

-- Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")

local SomeText = "FireClient"

local function EnterPlayer(player)
    S2C_DeadEvent:FireClient(player, SomeText) -- Passing arguments
end
Players.PlayerAdded:Connect(EnterPlayer)


-----------------------------------------------------------------------------------
-- LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")

local function OnDeadEvent(text)
    print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)

FireAllClients

Fire an event to the All players client.

Parameters

Tuple Arguments

Return

void

Code Samples

-- Script
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")

local function EnterPlayer(player)
    local SomeText = "FireAllClients"
    S2C_DeadEvent:FireAllClients(SomeText) -- Passing arguments
end
Players.PlayerAdded:Connect(EnterPlayer)


-----------------------------------------------------------------------------------
-- LocalScript
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")

local function OnDeadEvent(text)
    print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)

Events

OnClientEvent

The OnClientEvent is an event that is fired when the server communicates with the client via RemoteEvent. It is commonly used to notify clients about specific server actions or send data from the server to the client. This event is useful for handling client-side logic based on server-triggered interactions or updates.

Parameters

Code Samples

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local S2C_DeadEvent = ReplicatedStorage:WaitForChild("S2C_DeadEvent")

local function OnDeadEvent(text)
    print("[OnDeadEvent]", "Parameter : ", text)
end
S2C_DeadEvent.OnClientEvent:Connect(OnDeadEvent)

OnServerEvent

OnServerEvent is an event that is triggered when the client communicates with the server using RemoteEvent. It is used for handling and processing data or actions sent by the client to the server. This event is particularly useful for managing server-side logic based on client-triggered interactions or requests.

Parameters

Code Samples

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local C2S_EnterPlayer = ReplicatedStorage:WaitForChild("C2S_EnterPlayer")

local function OnEnterPlayer(player, number)
    print("[OnEnterPlayer]", player.Name, " / Parameter : ", number)
end
C2S_EnterPlayer.OnServerEvent:Connect(OnEnterPlayer)