Script Keeps Restarting when a Click Event Occurs

Im trying to make a script that will run when a player clicks a part.
The Script duplicates itslef and runs again when the player clicks on the part.

Whenever I search up something like, “Keep script from repeating” or “Script Repeats on event” the only stuff that pops up is how to make it occur, I don’t want this to occur, any suggestions?

the script Outline since its massive:

local screen = game.workspace.GasStation.Register.Screen
screen.ClickDetector.MouseClick:Connect(function(click)
   -- script body
end)

Has to be in ServerScriptService so everyone can see the changes to the parts in workspace.

well you forgot a )

local screen = game.workspace.GasStation.Register.Screen
screen.ClickDetector.MouseClick:Connect(function(click)
   -- script body
end)

I had that in the origional script, but typed it all since it was massive. Thanks.

Or use mousebutton1click that might help

Mousebutton1click isnt for click detectors I dont think.

Are you perhaps looking for a debounce? For example

local Part = script.Parent;
local Debounce = true; -- Boolean determining whether the program can execute again or not after a duration

Part.Touched:Connect(function()
    if (Debounce) then -- Can the program execute?
        print("I was stepped on! Ouch!"); -- Output that the program executed
        Debounce = false; -- Prevent the program from executing again
        wait(1); -- Wait 1 second
        print("Ready to be stepped on again! :<"); -- Output that the program can be executed again
        Debounce = true; -- Allow the program to execute again
    end
end);

I hope this helped. :slight_smile:

Oh, thats what I needed, thanks.

1 Like

By the way, for a debounce I will personnally use tick()
It is more efficient than doing wait()

local Part = script.Parent
local Debounce = tick()

Part.Touched:Connect(function()
    if (tick() - Debounce) >= then 
        print("I was stepped on! Ouch!")
       Debounce = tick()
end);

Apologies for the delayed response. :slight_smile: Sort of off-topic but I wanted to give a reply.

Using tick may be more efficient, but there’s quite a bit you would have to taken into account if you were to use it.

  1. How long your code takes to execute - if you’re using a yielding function or code that uses wait, it’s going to be able to execute again while the previous code may still be running.
  2. There’s the risk of having the code execute more than once - yes, to counteract this you could put the debounce at the top, but you run into the issue with #1.
  3. It doesn’t have as much control - with the boolean approach, you could have the debounce reenabled after a set of requirements are met. With the tick approach, it’s not really possible.
  4. tick is going to be deprecated in the future, so it’s recommended to use os.clock or os.time instead.

I think the tick approach would be use-case specific - I wouldn’t recommend using it actively.

EDIT
The post you replied with is about wait() as stated in the post itself, not wait(n). The use of wait(n) is fine as long as you handle it with care.

Well, using tick() is way more efficient, Avoiding wait() and why