Problems with making a GUI disappear after a certain number of seconds

  1. What do you want to achieve? Keep it simple and clear!
    I have a GUI which is visible only when the server fires a client event and then disappears after a certain number of seconds.

  2. What is the issue? Include screenshots / videos if possible!
    If another event is fired while the countdown for the GUI to disappears is still going on, the GUI will only appear briefly before being made invisible again rather than the timer being reset.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Here some code from the client script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes

[...]

Remotes.Dialogue.OnClientEvent:Connect(function(Speaker, Speech, DisplayFor)
	Dialogue.Visible = true
	
	Dialogue.Speaker.Text = Speaker
	Dialogue.Speech.Text = Speech
	
	wait(DisplayFor)
	Dialogue.Visible = false
end)

Try this (there’s probably a more efficient way but it should get the job done):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes

[...]

local dontClose = false
local ran = false
Remotes.Dialogue.OnClientEvent:Connect(function(Speaker, Speech, DisplayFor)
    if ran == true then
        dontClose = true
    end
    ran = true
    Dialogue.Visible = true
	
    Dialogue.Speaker.Text = Speaker
    Dialogue.Speech.Text = Speech
	
    wait(DisplayFor)
    if dontClose == true then
        dontClose = false
    else
        Dialogue.Visible = false
    end
    ran = false
end)

Depending on how the event is called this could leave the dialogue always visible say if it is called with a 2 second delay and immediately called with a 1 second delay.

That is true, mb. Didn’t really put too much thought into that answer.

No worries this isn’t as trivial as it seems. If the delay is always the same your code will work. This is about all I can think of:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes

[...]

local count = 0

Remotes.Dialogue.OnClientEvent:Connect(function(Speaker, Speech, DisplayFor)
    count += 1
	Dialogue.Visible = true
	
	Dialogue.Speaker.Text = Speaker
	Dialogue.Speech.Text = Speech
	
	task.wait(DisplayFor)
    count -= 1
    if count == 0 then
	    Dialogue.Visible = false
    end
end)
1 Like

make event clones the gui, it better, and use corountines to do many things at the same time, it’s easier that firing remote to one gui, make state or something like that, or maybe cooldovvn