PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

OnServerEvent

Fires listening functions in Script when RemoteEvent/FireServer is called from a LocalScript.

This is used to retrieve remote events fired by the client and intended for the server. This event is in place to provide a method for communicating between the client and server, which is well documented in this article. This event retrieves remote events fired by the client to the server.

To fire from the server to the client, you should use RemoteEvent/FireClient and RemoteEvent/OnClientEvent.

Parameters

Name Type Default Description

The Player associated with the client that the FireServer call originates from.

The arguments passed by the FireServer method.


Code Samples


RemoteEvents: Client to Server

In order for a client to send a message to the server, it needs to fire the RemoteEvent with the function RemoteEvent/FireServer in a LocalScript.

The server meanwhile needs to connect a function to the OnServerEvent of the RemoteEvent.

When a client calls the FireServer function, any functions on the server that are connected to OnServerEvent will fire. Note that this is not immediate - the network connection between the client and server will determine how quickly this happens.

When firing a RemoteEvent from a client to the server, data can be included in the firing. By default, the functions connected to OnServerEvent will be passed the player who fired the event as the first parameter. If any other arguments are provided in the FireServer function, they will also be included in OnServerEvent after the player argument.

-- LocalScript
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
 
createPartEvent:FireServer(BrickColor.Green(), Vector3.new(10, 20, 0))


-- ========================================


-- Script
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"
 
local function onCreatePartFired(player, color, position)
	print(player.Name, "wants to create a part")
	local newPart = Instance.new("Part")
	newPart.BrickColor = color
	newPart.Position = position
	newPart.Parent = game.Workspace
end
 
createPartEvent.OnServerEvent:Connect(onCreatePartFired)