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.
script.Parent.OnServerEvent:Connect(function()
(The code it has to run).
wait(60)
(And then it destroys the object).
end)
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.
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)
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.
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)
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.
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.