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

OnClientInvoke

RemoteFunction

Tuple

The OnClientInvoke event fires the bound functions in LocalScripts when RemoteFunction/InvokeClient is called by the server from a Script. When the bound function returns, the returned values are sent back to the server.

This is used to listen to remote functions invoked by the server and intended for the client. This callback is in place to provide a method for communicating between the server and client.

To fire from the client to the server, you should use RemoteFunction/InvokeServer and RemoteFunction/OnServerInvoke.

Parameters

Name Type Default Description

Correspond with the arguments passed by RemoteFunction/InvokeClient.

Expected Return Type

Return Type Summary

Values returned by the function are returned by RemoteFunction/InvokeClient (back to the server).


Code Samples


RemoteFunctions: Server to Client

Invoking a client is very similar to invoking the server, except in this case the invocation has to pass the player to invoke. The function used to do this is RemoteFunction/InvokeClient.

The client listens for this function to be invoked by binding a function to RemoteFunction/OnClientInvoke using the assignment operator =, and not with an event within a LocalScript. When the player (client) is invoked, it will execute the bound function.

-- Server
 
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local createPopupRequest = Instance.new("RemoteFunction")
createPopupRequest.Name = "CreatePopupRequest"
createPopupRequest.Parent = ReplicatedStorage
Players.CharacterAutoLoads = false
 
local function onPlayerAdded(player)
	createPopupRequest:InvokeClient(player)
	player:LoadCharacter()
end
 
Players.PlayerAdded:Connect(onPlayerAdded)


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


-- LocalScript
 
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local createPopupRequest = ReplicatedStorage:WaitForChild("CreatePopupRequest")
 
local function onCreatePopupRequested()
	local screen = Instance.new("ScreenGui")
	screen.Parent = playerGui
	local closeButton = Instance.new("TextButton")
	closeButton.Text = "Welcome to the game! Click me to play!"
	closeButton.Size = UDim2.new(0, 300, 0, 50)
	closeButton.Parent = screen
 
	closeButton.MouseButton1Click:Wait()
	closeButton.Visible = false
end
 
createPopupRequest.OnClientInvoke = onCreatePopupRequested