Making a script execute only once every X seconds

  1. So in my game i have a problem with car button mass spawning and lagging the servers a lot.

  2. The problem is that some people click many times and it just spawn a bunch of them and they stack up.

  3. I was thinking of adding a wait() instance, but after i was thinking, yeah this could work, but after thinking, they could stack up and just work like normally and lag and nothing in favour.

-- Local script under the button.
script.Parent.MouseButton1Click:Connect(function(player)
	game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
end)

That should already only activate once per click.

The issue is probably another, check the scripts’ activity (F9)

local debounce = false
--mouse click function here
if debounce == true then return end 
debounce = true
wait(x)
debounce = false

Just add a debounce to prevent multiple cars from spawning at once?

-- Local script under the button.
local DB = false
script.Parent.MouseButton1Click:Connect(function(player)
    if DB == false then 
        DB = true
    	game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
        wait(10)
        DB = false
    end
end)

You could also implement a sanity check if there’s already a car touching the spawn area using GetTouchingParts() maybe?

Or he could move the cooldown to the server. But your idea is also plausible.

Even better, make the button click a server script.

Perhaps, you’d need to implement some sanity check on the server though for every player that attempts to click the button

I was just giving some random example

1 Like

i’ll try this out, thanks! also the server script has something that if player spawns a car, the older one get deleted, but players managed to bypass that and mass spawn them really quickly.

Everyone’s methods will work as they incorporate a debounce, but I’ll just mention my version as well

local DB = false

script.Parent.MouseButton1Click:Connect(function()
    if DB then return end
	DB = true
	game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
	wait(10)
	DB = false
end)

I always believe that if you have an if statement that doesn’t care about the other results, like in this cause, a guard clause should be used to help improve the readability of the code as there will be less indentation.

Also I"m surprised that no one noticed @OP tried to get the player instance from MouseButton1Click, which will not work as it returns the x and y coordinates of where you clicked when the event was activated

while wait(x) do
-- Local script under the button.


 script.Parent.MouseButton1Click:Connect(function 
         (player)
  	 
    game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
 end)

No needed lots of work. @EmbatTheHybrid

I do not understand what are you trying to do, that’s literally not gonna help at all as it’ll just make another MouseButton1Click event, it wont debounce it at all, the issue will still happen

@GamebringerDev

There’s no need to be using a loop, the Event will fire when the button is first clicked on so your code isn’t necessary to add that little bit more

@EmbatTheHybrid

MouseButton1Click does not return back any parameters when it is fired, I believe it’s Tuple arguments only

https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Click

Huh? That’s odd, could’ve sworn it returned stuff, I’ll need to check that up

Here’s an idea

local DB = false
local TimeToWait = 10

script.Parent.MouseButton1Click:Connect(function(player)
    if DB == false then
        game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
        DB = true
        wait(TimeToWait)
        DB = false
    else
        print("You cannot spawn a car right now!")
    end
end)
1 Like