(Roblox Studio) Client-Server-Client 通信无法工作

我正在尝试使用服务器端的脚本通过 RemoteEvent 从一个 LocalScript 发送数据到另一个 LocalScript。第一个 LocalScript 在 StarterCharacterScripts 中,服务器端脚本在工作场所中,第二个 LocalScript 在玩家角色模型下方

我在每个函数中设置了打印语句以确定通信停止的位置。在服务器向第二个 LocalScript 触发函数时,它停止工作。然而,我无法确定原因。

第一个 LocalScript(StarterCharacterScripts)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = ReplicatedStorage:WaitForChild("CreatePartEvent")
local UserInputService=game:GetService("UserInputService")
local player=game.Players.LocalPlayer
UserInputService.InputBegan:Connect(function(input,gameProcessed)
if input.KeyCode==Enum.KeyCode.G then
createPartEvent:FireServer(251,226,11)
print("Bing!")
end
end)

服务器端脚本(工作场所)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "CreatePartEvent"
local fadePartEvent = Instance.new("RemoteEvent")
fadePartEvent.Parent=game.ReplicatedStorage
fadePartEvent.Name="FadePartEvent"

local function onCreatePartFired(player,red,green,blue)
print("Ting!" ,player,red,green,blue)
fadePartEvent:FireClient(player,red,green,blue)
end

createPartEvent.OnServerEvent:Connect(onCreatePartFired)

第二个 LocalScript(玩家模型)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player=game.LocalPlayer
local fadePartEvent=ReplicatedStorage:WaitForChild("FadePartEvent")
fadePartEvent.Name="FadePartEvent"
local function onFadePartFired(player,red,green,blue)
print("Done!")
print(player,red,green,blue)
end

fadePartEvent.OnClientEvent:Connect(onFadePartFired)

如果代码正常运行,则输出应为:

Bing!
Ting! BusterTornado 251 226 11
Done! BusterTornado 251 226 11

但是,输出显示为:

Bing!
Ting! BusterTornado 251 226 11

这表明该函数未到达第二个 LocalScript。我尝试过搜索但还没有找到答案。

点赞
用户12092008
用户12092008

你只需要使用 BindableEvent,它可以在 [服务器端到服务器端] 或者 [客户端到客户端] 之间发送数据。 在发送数据的 LocalScript 上使用 :Fire,然后在第二个 LocalScript 中使用 .Event。

2019-09-19 18:20:33