Wait() for every time the Touch Functions are Called; not working properly [SOLVED]

Hello. I am trying to make two touch functions have a cooldown for every time they are both fired, by a player who has a specific tool. The first touch function makes the SurfaceGui enabled, whilst the second touch function does the opposite. I want to use any form of wait(), to prevent the SurfaceGui from flickering on and off, when someone is touching the board using the tool. This is so I can have a duration where it waits in between, every time the touch function runs.

However, the problem is, none of them are working as I intend them to. Some of the functions also keep repeating themself after they wait once, even when nobody is touching the part. Some of them even cause the game to lag out, when tested.

I have tried using while task.wait() do, only wait() on it’s own, or looped a debounce for a number of seconds in a loop, etc. If I take out the while task.wait() do altogether, the wait() still seems to only repeat once.

This is the part of the script, that I am having problems with:

			if marker ~= nil and marker.Parent ~= nil and marker.Parent:FindFirstChild("CardNumber") ~= nil and marker.Parent.CardNumber.Value == 55 then
				copy.Enabled = true
			    copy.ImageButton.LocalScript.Disabled = false
				if marker ~= nil and marker.Parent ~= nil and marker.Parent:FindFirstChild("CardNumber") ~= nil and marker.Parent.CardNumber.Value == 55 then
					copy.Enabled = false
					copy.ImageButton.LocalScript.Disabled = true
					copy.Enabled = false
				    copy.ImageButton.LocalScript.Disabled = true
  • The downside to the script above, is that it seems to automatically disable after one touch. I want to be able to touch it once and then have to touch it again whilst holding the same tool to disable it. Then the script should repeat itself and start again.

  • Nothing on YouTube, or anywhere else has helped me solve this problem.

My Question are:

  1. What might I be doing wrong with the wait() here, for it to only repeat once regardless of whether I use a while loop or not?
  2. What are the best solutions in this senario, and what would I need to change, for it to work properly?
  3. Should I keep it in a Script or another LocalScript?

All replies and solutions are appreciated!


Can’t you just do something like:

Local debounce = false
If not debounce then
debounce = true
SurfaceGui.Enabled = true
Surface.Enabled = false
debounce = false

I know this code has some flaws but this should help


Weird. It works for the first function for the first time, but whenever I try to put it back on from the second function it stays as disabled. maybe I just set it up wrong and that’s why it doesn’t work

I think it’s because I have more than one function which doesn’t help.

1 Like

Yeah so my advise is to remove the second .Touched event.

It’s unnecessary since there is no reason to create a second .Touched event in the same function.


Wait. If I remove that it seems to just run onto the next block without the player touching the board again. Whereas if I keep it, it eventually flickers again.

I want it to stay on until you touch it the 2nd time, but not for it to automatically turn off.

1 Like

I think adding “debounce” may work.


The same debounce for both blocks of code?


Yeah i don’t really know but i think so.

1 Like

This code is going to produce a memory leak due to the nested event connections.

1 Like

What type of memory is susceptible for it to be leaked?

1 Like

So we are stepping on boards, from one board to the next … and you want a GUI to show only when you’re on a board? Or more of a delay on when the GUI is disabled?

1 Like

Every time you :Connect it creates a RBXScriptsignal like Instance.new() which takes memory unless it’s :Disconnected or the script is destroyed.

I believe you are looking for something like this with touch and touch ended for enabling GUI.

It should do the job and if not there is the alternative listed as Zone+ region3.


Firstly, the board is disabled. I want to enable it when I touch the board the first time with a tool (that works). Then it will wait for a few seconds before you can touch it again (with the same tool) and for it to become disabled.

The cycle loops when you touch it for the 3rd time.

Enable it as in walk on it? Like steps … (that turn on and off)

Just by touching it, but I don’t want to be able to keep it touched and for the frame to flicker on and off. That’s why I am using wait(), so even if you keep touching it without moving, it would only enable the frame in a given period of time.

The board is on a wall.

Would I do that two times, since I have more than one touch event or just use one in the example?

Furthermore, if I have two does it matter if I introduce two touch events together and then end the two touch_ended events together? Or do I have to end one before I can create a new one again?

I wouldn’t put a function inside a function for this. This is tricky because of touch. If you stop on the object it’s like resetting, as soon as you move it will fire again. So you need flags like a debounce.
Here is the same thing-ish … showing the same thing. Don’t matter how many times it’s touched, it will not fire again until after it has completed.

local flag = true
if touched then
if flag == true then flag = false
– code
flag = true

Need that just to get a handle on what is getting touched and when.
Now that you have solid testing setup. You can always set up a counter(s) or even a Boolvalue on the Explorer to test off/set for result. Even set up a toggle switch like this.

1 Like

First of all, you should not have 2 .Touched event it is completely unnecessary.

Second of all, from my understandings, you want it so when you touch a part, a gui appears.
But the second time you touch it, the gui disappears, correct?

Yes. The problem I have is when I touch it the first time and wait 5 seconds the frame disappears without me having to touch it the 2nd time. This is without the second .Touch Event but when I include that as well I seem to be getting problems.

I will, however try everybody’s script and see how they all run.

1 Like

That sounds like my script, i knew it was gonna act like that, but to solve your issue, you’re gonna need to do some stuff with .Touch and .TouchEnded.

I can’t create a script right now cuz i’m on vacation.

1 Like