如何使用远程事件与远程函数

有些操作只能在服务器中运行,而有些却只能通过客户端进行。

例如,玩家点击按钮后,在游戏世界生成一个怪物。

这时候就需要服务器与客户端进行通信。开发者就可以使用 远程事件RemoteEvent)或 远程函数RemoteFunction)。

远程事件与远程函数

一、共同点

两者都用于客户端与服务器之间的通信

二、不同点

  1. 远程事件(RemotoEvent):属于单向通信,可以发送携带信息的事件
  • 客户端->服务器示例:玩家按下F键按钮后,体型变大两倍。
  • 服务器->客户端示例:当Boss出生时,改变指定玩家UI
  • 服务器->客户端示例:当Boss出生时,改变所有玩家UI
  • 客户端->客户端示例:当玩家修改宠物名称时,同步给其他玩家
  1. 远程函数(RemoteFunction):属于双向通信,需要等待响应后才会继续
  • 客户端->服务器->客户端示例:检测目标是否命中,客户端开枪后,服务器进行检测,并返回命中目标的对应信息
  • 服务器->客户端->服务器示例:此方法不推荐使用,因为客户端可能因为各种原因无响应,会导致服务器脚本等待响应不会继续运行

如何使用远程事件

1. 首先需要创建RemoteEvent 节点。

  • 在服务器和客户端都可访问的位置新建 RemoteEvent 节点,主要在MainStorage节点下,创建RemoteEvent节点,命名为RemotoEventTest

2. 客户端向服务器通信

LocalScript所进行的更改时影响客户端的,如果客户端对服务器进行更改,则需要发送请求。使用通信在服务器创建一个新的模型

  • 首先在MainStorage中创建一个RomoteEvent并命名为TestForRemotoEvent
  • StartPlayer中创建LocalScript,使用创建的RemoteEvent实例,使用FireServer()通信

实例:客户端发送消息通知服务器创建一个基础模型(Script脚本需要挂载至一个模型下方)

客户端脚本(LocalScript)
--获取MainStorage
local mainStorageService = game:GetService("MainStorage")
---获取该节点下的TestForRemoteEvent
local remoteEvent = mainStorageService:WaitForChild("TestForRemoteEvent")
--与服务器通信并携带位置信息
remoteEvent:fireServer(Vector3.New(100,100,100))
服务器脚本(Script)
local mainStorageService = game:GetService("MainStorage")

local remoteEvent = mainStorageService:WaitForChild("TestForRemoteEvent")
--获取需要创建子节点的模型
local Model = script.Parent
--创建新的基础模型
remoteEvent.OnServerNotify:Connect(function(uin,Pos)
	local cube = SandboxNode.New("GeoSolid",Model)
      --使用传递的位置信息
	cube.LocalPosition = Pos
      --设置创建的基础模型的形状
	cube.GeoSolidShape = Enum.GeoSolidShape.Cuboid
      print(uin)
end)

客户端与服务器通信时,自带玩家迷你号uin的信息

3. 服务器向客户端通信

当服务器发生某些改变后需要通知指定玩家(客户端),在 ServerScriptService 中创建 Script 脚本,使用创建的 RemoteEvent 实例,使用 firefireClient(uin) 通信(该方法使用时需要指定客户端)

实例:当一个玩家加入至游戏内,发送给该玩家指定信息

服务器脚本(Script)
--获取MainStorage服务
local MainStorage =game:GetService("MainStorage") 
--获取玩家服务
local players = game:GetService("Players")      
--获取RemoteEvent实例
local remoteEvent = MainStorage:WaitForChild("TestForRemoteEvent") 
--需要携带的信息
local a = 544     

local function onPlayerAdded(player)
      --通信事件
      remoteEvent:FireClient(player.UserId,a) 
end
--当玩家加入时发送
players.PlayerAdded:Connect(onPlayerAdded) 
客户端脚本(LocalScript)
--获取MainStorage服务
local MainStorage =game:GetService("MainStorage") 
--获取RemoteEvent实例
local remoteEvent = MainStorage:WaitForChild("TestForRemoteEvent") 

local function onNotify(str)
	print(str)
end
--客户端接收的时候执行打印操作
remoteEvent.OnClientNotify:Connect(onNotify) 

4. 服务器对所有客户端

当服务器发生某些改变后需要通知所有玩家(客户端),在 ServerScriptService 中创建 Script 脚本,使用创建的 RemoteEvent 实例,使用 firefireClient(uin) 通信(该方法使用时需要指定客户端)。与指定玩家不同,该方法不需要 uin ,会对当前所有连接的客户端发送信息

实例:当一个玩家加入至游戏内,将玩家迷你号发送个所有客户端

服务器脚本(Script)
--获取MainStorage服务
local MainStorage =game:GetService("MainStorage") 
--获取玩家服务
local players = game:GetService("Players")      
--获取RemoteEvent实例
local remoteEvent = MainStorage:WaitForChild("TestForRemoteEvent") 
--需要携带的信息
local a = 544     

local function onPlayerAdded(player)
      --通信事件
	remoteEvent:FireClient(player.UserId,a) 
end
--当玩家加入时发送
players.PlayerAdded:Connect(onPlayerAdded) 
客户端脚本(LocalScript)
--获取MainStorage服务
local MainStorage =game:GetService("MainStorage") 
--获取RemoteEvent实例
local remoteEvent = MainStorage:WaitForChild("TestForRemoteEvent") 

local function onNotify(str)
	print(str)
end
--客户端接收的时候执行打印操作
remoteEvent.OnClientNotify:Connect(onNotify) 

如何使用远程函数

远程事件 对比, 远程函数 需要等待另一边的响应后才能继续运行。类似于远程事件,需要现在 MainStorage 创建 RemoteFunction ,命名为 TestForRemoteFunction

  • 客户端->服务器->客户端

    主要用于需要服务器执行且需要等待回应的逻辑,则可以通过 RemoteFunction

    实例:客户端发送一串字符,服务器进行解析打印后再返回给客户端新的字符

客户端脚本(LocalScript)
--客户端的调用脚本
local mainStorageService = game:GetService("MainStorage") 

local testf1 = mainStorageService:WaitForChild("TestForRemoteFunction")

local ret = testf1:InvokeServer("ddddddddddabcdd")

print(ret)
服务器脚本(Script)

local mainStorageService = game:GetService("MainStorage")

local testf1 = mainStorageService:WaitForChild("TestForRemoteFunction")

testf1.OnServerInvoke = function(uin, arg1)

    print(" recv TestForRemoteFunction  from client:"..tostring(uin).. " (arg1:) ".. tostring(arg1))

    return "helloinvoke"
end
Last Updated: