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

InvokeClient

Calls the method bound to the RemoteFunction by RemoteFunction/OnClientInvoke for the given Player. Use from a Script.

If the result is not needed then it is recommended to use a RemoteEvent/FireClient instead, as its call is asynchronous and doesn’t need to wait for a response to continue execution.

This is used to bind functions to invoke the client when the remote event is invoked by the server. This function is in place to provide a method for communicating between the client and server, which is well documented in this article.

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

Note

In practice, the server does not often invoke the client. Clients typically do not have information the server doesn’t have and the actions that only a client can take (displaying a GUI for instance), often do not require a callback. That said, the server invoking clients is still an action that the Roblox engine will support and may be useful in niche situations.

Warning

If a client disconnects or leaves the game while it is being invoked from the server, the InvokeClient function will error. It is therefore recommended to wrap this function in a pcall so it does stop the execution of other code.

Parameters

Name Type Default Description

The Player that the remote function is being fired to.

The arguments passed to the RemoteEvent/OnClientInvoke method.

Returns

Return Type Summary

Values returned by RemoteFunction/OnClientInvoke.


Code Samples


RemoteFunctions: Client to Server

When a client wants to use a remote function, it will invoke the server with the RemoteFunction/InvokeServer function within a LocalScript.

The server listens for this function to be invoked by binding a function to RemoteFunction/OnServerInvoke using the assignment operator =, and not with an event within a Script. When the server is invoked, it will execute the bound function.

-- LocalScript
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local createPartRequest = ReplicatedStorage:WaitForChild("CreatePartRequest")
 
local newPart = createPartRequest:InvokeServer()
print("The server created this part for me:", newPart)


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


-- Script
 
local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"
 
local function onCreatePartRequested(player)
	print(player.Name, "wants to create a new part")
	local newPart = Instance.new("Part")
	newPart.Parent = game.Workspace
	return newPart
end
 
createPartRequest.OnServerInvoke = onCreatePartRequested