Search code examples
luaroblox

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


I am using a "wave" script spawning a random group of zombies every 2 minutes. After that time, the zombies still alive are removed from Workspace and replaced by another random group. This can go on forever.

One of the zombies in every group has a script that fires an event, which is placed (clones itself) in ReplicatedStorage, then the event is removed after 3 seconds to avoid duplicates.

I have a Gui that popup once it detects the presence of the Event in ReplicatedStorage whenever a new group of zombies appears in Workspace. The way I am doing it is by placing a local script and a Gui in StarterGui. It's a simple Gui with a frame that is not set to visible when I launch the game because I want the players to see the Gui only where a new wave appears.

So far so good, everything is working like a charm (first group appear, fires the event and then a popup message apears and then disappears after 3 seconds).. yup. All good...excepted for one thing...the Gui will appear only once. I need that Gui to appear for all players and everytime a new wave appears and then removed after 3 seconds.

I hope I explained correctly what I need. So here's the code of the local script I placed in StarterGui. I spent days trying to solve this.

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

thank you!


Solution

  • This is the way your code works right now :

    1. Wait for the existence of an object in ReplicatedStorage called ZombieEvent. (Your zombie script clones in the object)
    2. Show the ZombieGui for 3 seconds, then hide it again
    3. While ZombieEvent continues to exist, wait. (This loop is eventually broken by other scripts deleting ZombieEvent)

    Then the script ends and that is the end of it, there isn't anything to tell the code to start over from the beginning or to loop back somehow.

    If you want this to work for your round-based gameplay, there are a few ways you could fix this, but here's what I would recommend :

    1. Keep a RemoteEvent in ReplicatedStorage called ZombieEvent, don't bother cloning it or deleting it every round.
    2. Connect a function to the RemoteEvent's OnClientEvent signal in your LocalScript.
    3. Have the special zombie fire the signal using the FireAllClients function.

    Here's what that would look like :

    -- 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)
    

    Then in your Zombie's Script, you would fire the signal like this :

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