每次触发事件时,如何使所有玩家都可以看到 screenGui 元素?

How to make a screenGui element visible for all players everytime an event is fired?

我正在使用一个 "wave" 脚本,每 2 分钟生成一组随机的僵尸。在那之后,仍然活着的僵尸将从工作区中移除,并由另一个随机组替换。这可以永远持续下去。

每个组中的一个僵尸都有一个触发事件的脚本,该脚本被放置(克隆自身)在 ReplicatedStorage 中,然后在 3 秒后删除该事件以避免重复。

我有一个 Gui,每当工作区中出现一组新的僵尸时,一旦它检测到 ReplicatedStorage 中存在事件,它就会弹出。我这样做的方法是在 StarterGui 中放置一个本地脚本和一个 Gui。这是一个带有框架的简单 Gui,当我启动游戏时未将其设置为可见,因为我希望玩家仅在出现新浪潮的地方看到 Gui。

到目前为止一切顺利,一切正常(第一组出现,触发事件,然后弹出消息出现,然后在 3 秒后消失)。是的。一切都很好……除了一件事……Gui 只会出现一次。我需要那个 Gui 出现在所有玩家面前,每次出现一个新的波浪,然后在 3 秒后移除。

我希望我正确解释了我的需要。所以这是我放在 StarterGui 中的本地脚本的代码。我花了几天时间试图解决这个问题。

local Event = game.ReplicatedStorage:WaitForChild("ZombieEvent")

--

player = game.Players.LocalPlayer

player.PlayerGui.ZombieGui.Frame.Visible = true
wait(3)
player.PlayerGui.ZombieGui.Frame.Visible = false


--

while Event do
    wait(.05)   
end

谢谢!

这就是您的代码现在的工作方式:

  1. 等待 ReplicatedStorage 中存在名为 ZombieEvent 的对象。 (您的僵尸脚本克隆在对象中)
  2. 显示 ZombieGui 3 秒,然后再次隐藏
  3. 虽然 ZombieEvent 继续存在,但请稍等。 (这个循环最终被其他删除ZombieEvent的脚本打破)

然后脚本结束,这就是它的结尾,没有任何东西可以告诉代码从头开始或以某种方式循环回来。

如果您希望它适用于基于回合的游戏玩法,您可以通过几种方法解决此问题,但这是我推荐的方法:

  1. 在 ReplicatedStorage 中保留一个名为 ZombieEvent 的 RemoteEvent,不要在每一轮都克隆或删除它。
  2. 将函数连接到 LocalScript 中的 RemoteEvent's OnClientEvent signal
  3. 让特殊僵尸使用FireAllClients function.
  4. 发射信号

这是它的样子:

-- Local Script
player = game.Players.LocalPlayer

local Event = game.ReplicatedStorage:FindFirstChild("ZombieEvent")
Event.OnClientEvent:Connect(function()
    player.PlayerGui.ZombieGui.Frame.Visible = true
    wait(3)
    player.PlayerGui.ZombieGui.Frame.Visible = false
end)

然后在你的 Zombie's Script 中,你会像这样发射信号:

-- tell everyone to show the screen
local Event = game.ReplicatedStorage:FindFirstChild("ZombieEvent")
Event:FireAllClients()