Cooldown on a RemoteEvent?

Ok, so I here I am trying to achive a remote event which is activated form a local script and then it spawns a block and it despawns after a while but it must have a server cooldown and not just a cooldown inside the local script.

I could use the Debounce method but the RemoteEvent already has to wait X seconds to do something so I would like some possible solutions. :thinking:

script.Parent.OnServerEvent:Connect(function()

 (The code it has to run).

wait(60)

 (And then it destroys the object).
end)

I did not understand what you are trying to achieve. Would you mind being more clear?

Are you trying to have each individual block despawn without the RemoteEvent yielding? If so, you could use the spawn() function, which would circumvent this issue.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
  spawn(function()
    -- run code
    wait(60)
    -- other code
  end)
end)

I have a local script that reads if I activate a ClickDetector. If I do it fires a RemoteEvent that spawns an object for 60 seconds and then it destroys it.

Connections are already run on distinct threads, there is no reason to put a spawn.

1 Like

Oh, really? Didn’t know that, lol

If that was the case, using Debris:AddItem() would be cleaner and more efficient.

1 Like

After the 60 seconds it despawns the “object”.

So you’re trying to spawn a block and destroy it after 60 seconds?

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
  local part = Instance.new("Part", workspace)
  game:GetService("Debris"):AddItem(part, 60) -- destroys the part after 60 seconds
end)

Yeah, kind of like that. It gets an object from the server storage and then it destroys it after 60 seconds

Are you trying to achieve a cooldown? If so, you can just add a debounce. I’m confused on what you are trying to achieve here

I can add a cooldown on the local script that fires the RemoteEvent. But that will be only per player, what I want it a cooldown for EVERY player.

For example if I were to press a button it would do the event but if another player tries to fire the event within 5 seconds for example it won’t work. Like a 5 second cooldown.

So you want a global cooldown, correct? If that’s the case, then a debounce stored in the server script should work just fine.

If you want a local cooldown, one suggestion I could offer is that you could store when each player activates the RemoteEvent in a table, and compare that value to the next time to activate it to see if a certain amount of time has passed.

1 Like

You can try somthing like this, I just grabed it from one of my server scripts, you store the player’s userid into a table

--Sanity Checks 
local sanityChecks = {}

DamageBlock.OnServerEvent:Connect(function(player, pos, instance, material)
	local userId = player.UserId

	if not sanityChecks[userId] then 
		sanityChecks[userId] = true
	end
	
	if sanityChecks[userId] == true then 
		sanityChecks[userId] = false 
		
		delay(2, function()
			sanityChecks[userId] = true
		end)



Yeah but how would a global cooldown work with debounce? The event already has to wait 60 seconds.

local CooldownStart = 0
script.Parent.OnServerEvent:Connect(function()
	if os.clock() - CooldownStart > COOLDOWN_LENGTH then
		CooldownStart = os.clock()

		-- do stuff
	end
end)

This is one way you can do cooldowns. os.clock returns a time in seconds. You check if the time between the last cooldown start is greater than the cooldown, and if so you run the code and set the cooldown start again.

1 Like

If you use Debris:AddItem(), the script shouldn’t have to wait.

Consider this:

local db = false

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
  if not db then
    db = true
    local part = Instance.new("Part", workspace)
    game:GetService("Debris"):AddItem(part, 60) -- destroys the part after 60 seconds
    wait(5)
    db = false
  end
end)

This script will activate one every 5 seconds. Debris:AddItem() doesn’t cause the current script to yield.

EDIT: oops forgot my ```

1 Like

Wow I think that’s what I have been looking for! One minute let me test it.

2 Likes