Issue with making cooldown

  1. What do you want to achieve? Keep it simple and clear!
    Topic says everything
  2. What is the issue? Include screenshots / videos if possible!
    My problem is what task.wait and more extended ways of delays, are not giving any result
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? i tryed using a cooldown with debounce, task.wait cooldown and etc., they are all not stopping script from executing

Client Code:

local player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local ClientFunctions = require(game:GetService("ReplicatedStorage").ClientFunctions)
local PhotonSpawn = workspace.Events.PhotonSpawn
local ReturnUpgrade = workspace.Events.ReturnUpgrade
UIS.InputBegan:Connect(function(inp, eng_proc)
	if eng_proc then	
	end
	if inp.KeyCode == Enum.KeyCode.E then
		PhotonSpawn:FireServer(ReturnUpgrade:InvokeServer(player, "PhotonUpgrades" ,"Bulk"))
		ClientFunctions:TextTransparent(player.PlayerGui.GameGUI.TextLabel, "increase", 150)
		ClientFunctions:TextTransparent(player.PlayerGui.GameGUI.TextLabel, "decrease", 150)
	end
end)

Server:

PhotonSpawn.OnServerEvent:Connect(function(player, amount)
	ServerFunctions:SpawnPhoton(player, amount)
end)

Module

local PlayerManager = require(script.Parent.PlayerManager)
local ServerFunctions = {}

function ServerFunctions:SpawnPhoton(player, amount)
	local Models = game:GetService("ServerStorage").Models
	PlayerManager.SetValue(player, "Values", "Photons", PlayerManager.GetValue(player, "Values", "Photons") + amount)
	for i=1,amount do
		local Clone = Models.Photon:Clone()
		Clone.Parent = workspace.FirstMap.Photons
	end
end

return ServerFunctions

in each of them i tryed to issue cooldown, but it never worked correctly. So i basically need help with debouncing it i guess

Could you be more specific? What script are you trying to stop? or what is the behaviour you are looking for with that system?

I suppose, you dont want the player to spam E to create photons?

First issue I see in here is that you are letting client to tell server how many photons server should place:

PhotonSpawn:FireServer(ReturnUpgrade:InvokeServer(player, "PhotonUpgrades" ,"Bulk"))

On client, you are asking to server by InvokeServer, Server returns a valid amount. Then Client/Exploiter fires the PhotonSpawn remote, with whatever value they want, idk inf*

Then Server listen the call and run the module function to create the photons exploiter said…:

PhotonSpawn.OnServerEvent:Connect(function(player, amount)
	ServerFunctions:SpawnPhoton(player, amount)
end)

So, you dont need the first InvokeServer, you can check the allowed amount for that player when the module is going to do the for i=1, amount do to clone the photons.


You could add the debounce inside that module too, to check that player is allowed to create more photons. You could use a table that stores the timestamp of the last time the player created photons, or a value somewhere that toggles after certain amount time since the last photon creation

First put a cooldown on the localscript

local player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local ClientFunctions = require(game:GetService("ReplicatedStorage").ClientFunctions)
local PhotonSpawn = workspace.Events.PhotonSpawn
local ReturnUpgrade = workspace.Events.ReturnUpgrade
-- cooldown for local player
local cooldowntime = 5
local cooldown = false
UIS.InputBegan:Connect(function(inp, eng_proc)
    if cooldown then return end
	if eng_proc then	
	end
	if inp.KeyCode == Enum.KeyCode.E then
        cooldown = true
		PhotonSpawn:FireServer(ReturnUpgrade:InvokeServer(player, "PhotonUpgrades" ,"Bulk"))
		ClientFunctions:TextTransparent(player.PlayerGui.GameGUI.TextLabel, "increase", 150)
		ClientFunctions:TextTransparent(player.PlayerGui.GameGUI.TextLabel, "decrease", 150)
        task.wait(cooldowntime)
        cooldown = false
	end
end)

Then you should also put a cooldown on the event to avoid any exploiter doing no cooldown shenanigans

local cd = {} -- seperate cooldown for each player since this is a not a local script
PhotonSpawn.OnServerEvent:Connect(function(player, amount)
    if table.find(cd, player) return end
    table.insert(cd, player)
	ServerFunctions:SpawnPhoton(player, amount)
    task.wait(5) -- same cd as in the localscript
    table.remove(cd, table.find(cd, player))
    -- thanks for pointing out the mistake devpeashie
end)

Was this what you were looking for?
Also as @Dev_Peashie said, the client shouldn’t tell the server how many photons should be produced

1 Like

Yup, thats how I would do it too.

Just something to add, table.remove(cd, player) wont work, cause it needs an int value. Would be:
table.remove(cd, table.find(cd, player))
Or a dictionary, almost the same.

Oops. My mistake there. Defenitely forgot to switch it to server.

I tryed doing cooldown for local player, but it still runs same?

What do you mean?
The function still runs even if cooldown is true?

Also those tables not really required, game is singleplayer(though maybe it will be multiplayer one day).

Yeah exactly. But other big problem is what i get cooldown from the server(like upgrade). Just realised that server check will protect it

If you get cooldowns from the server, maybe you should use make a remote event and fireclient to put the thing on cooldown if that helps

I’m not sure that I understood your statement correctly

I have currently added an client sided function(in client module) to return some client data, as like as cooldown, but also issue that cooldown on server in same time. And i assume it works correctly. Thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.