Disabling A GUI after a certain time makes it glitch

I have to post this again because for some reason no one gets the problem :frowning:

Summary: I have a GUI that enables itself when a RemoteEvent is called. 15 seconds after this RemoteEvent is called/ the gui is enabled, it because unenabled.

Problem: In most situations, after 15 seconds the player is still firing the GUI, making it enabled. Because of the constant back of forth from the deactivation (from the remote event 15 seconds ago) and the activation (from the latest remoteevent), it has a flickering event, seen in the video.

External Media

I have an idea for a solution but I don’t know how to script it. My idea is to make it de activate after 15 seconds after the LATEST RemoteEvent, and not EACH INDIVIDUAL RemoteEvent. I need help scripting this cuz i have no idea what to do.

Here is the script for the UI:

event.OnClientEvent:Connect(function(part, minus)
	if part:IsA("Terrain") == false then
		active = true
		--print("Client recieved.")
		--print(part.Name)
		--print(tostring(minus))

		local hp:number = UIFunction.GetHp(part)

		local maxHp = UIFunction.getMaxHp(part)

		if hp then UIFunction.Damage(part, minus, hp) end
		UIFunction.UpdateUI(part.Name, maxHp, hp)

		if hp == 0 then
			UIFunction.Finish(part)
		end

		wait(15)

		player.PlayerGui:WaitForChild("BreakingBar").Enabled = false
	end
end)

For clarity (Because last time I asked no one understood it…):
UIFunction.UpdateUI() is the one that enables it.

The wait(15) and the last line is the one that de activates it.
I do not have a problem with disabling it on each time. I need help de activating it on the LATEST RemoteEvent. The de activation has nothing to do with the if hp == 0 then thread.

Thank you for the help :slight_smile:

Are you trying to disable the GUI after 15 seconds from the last input only or is it just supposed to disable when the player is a certain distance from the thing you are hitting?

Think I see the problem and would explain the video.
I take it Connect(function is firing evetime the player swings, It is checking Terrain.
So if they hit Terrain it set up the UI (functions), waits 15 seconds then disables the bar.
Problem is this is happening with every swing, so it’s waiting mutable times.
I could be wrong …

local db=true
event.OnClientEvent:Connect(function(part, minus)
	if part:IsA("Terrain") == false then
		active = true
		--print("Client recieved.")
		--print(part.Name)
		--print(tostring(minus))

		local hp:number = UIFunction.GetHp(part)

		local maxHp = UIFunction.getMaxHp(part)

		if hp then UIFunction.Damage(part, minus, hp) end
		UIFunction.UpdateUI(part.Name, maxHp, hp)

		if hp == 0 then
			UIFunction.Finish(part)
		end
		
		if db then db = false wait(15)
			player.PlayerGui:WaitForChild("BreakingBar").Enabled = false
			db=true
		end		
	end
end)

might work, would have to be able to test to know

1 Like

After 15 seconds only. Sorry for the late reply

its because each time when the client recevies the event, it does not wait for the other one to finish. For example, let’s say you have 3 calls to it at different times.

Call 1: 0 seconds

Call 2 : 5 seconds later from call 1

Call 3 : 10 seconds later from call 1

//------//
call 1: disables gui

5 sec later, call 2: disables gui

10 sec later, call 3: disables gui
//------//

no matter what, it will STILL go through the wait. it does not wait for the last call to finish. you need to add checks and update it globally instead of locally in that function.