Help with cooldowns n such

  1. What do you want to achieve? I want to get a cloaker ability from pd2

  2. What is the issue? I want it to stop running after you click / 4.5 seconds passed. But whenever I click too fast, the next time I activate the ability, the 4.5 second timer blocks it off.

  3. What solutions have you tried so far? I tried using some confusing shenanigans but those didn’t work so now I’m here.

if input.KeyCode == Enum.KeyCode.Q and not isCharging and canPlay then
		canShoot = false
		canSwitch = false
		canReload = false
		isCharging = true
		
		chargeEvent:FireServer(true)
		
		for i,v in pairs(framework.vm:GetDescendants()) do
			if v:IsA("BasePart") then
				v:SetAttribute("OldTransparency", v.Transparency)
				v.Transparency = 0.999999
			end
		end
		
		autoRunning = run.RenderStepped:Connect(function(dt)
			hum:Move(Vector3.new(0,0,-1), true)
		end)
		
		task.wait(4.5)
		
		if isCharging then
			isCharging = false
			autoRun = false
			canShoot = true
			canSwitch = true
			canReload = true
			autoRunning:Disconnect()
			chargeEvent:FireServer(false)
		end
	end
isCharging = false
autoRun = false
canShoot = true
canSwitch = true
canReload = true
autoRunning:Disconnect()
chargeEvent:FireServer(false)

(the script above is inside a mouse.button1down fuction)

3 Likes

Create a debounce variable so it has to wait 4.5 seconds before u can click again

2 Likes

To add on to what @Earthraphobic2 said, make a debounce, but make sure it is defined outside of the function. you defined the canShoot, canSwitch, canReload, and isCharging inside the input and that will break your code since every time the key is pressed it will reload those variables, make sure to define it outside before the input code.

2 Likes

those aren’t the important variables that affect the cloaker thing, they’re part of my gun system

3 Likes

will try then, thanks for the tip

4 Likes

I think this is what you need to do (from what I understood), let me know if this is what you need.

local debounce = false

if input.KeyCode == Enum.KeyCode.Q and not isCharging and canPlay and not debounce then
    print("Activating ability...")  -- Debug print statement
    
    debounce = true
    
    canShoot = false
    canSwitch = false
    canReload = false
    isCharging = true
    
    chargeEvent:FireServer(true)
    
    for i,v in pairs(framework.vm:GetDescendants()) do
        if v:IsA("BasePart") then
            v:SetAttribute("OldTransparency", v.Transparency)
            v.Transparency = 0.999999
        end
    end
    
    autoRunning = run.RenderStepped:Connect(function(dt)
        hum:Move(Vector3.new(0,0,-1), true)
    end)
    
    print("Waiting for 4.5 seconds...")  -- Debug print statement
    wait(4.5)
    
    if isCharging then
        print("Deactivating ability...")  -- Debug print statement
        
        isCharging = false
        autoRun = false
        canShoot = true
        canSwitch = true
        canReload = true
        autoRunning:Disconnect()
        chargeEvent:FireServer(false)
    end
    
    debounce = false
end

I’m sorry if I didn’t explain correctly, but that’s not really the thing I strive for. I want the lines after the task.wait(4.5) to not interrupt the next time I use the ability

1 Like

here, the 4.5 second cooldown affects the NEXT time I activate the ability
(if I press left click too early that is)

1 Like

Oh, I see. So you’ll need to do this in your script.
In this way, the ability can be reused after a 4.5-second wait :smiley:

local isAbilityActive = false

if input.KeyCode == Enum.KeyCode.Q and not isAbilityActive then
    print("Activating ability...")  -- Debug print statement
    
    isAbilityActive = true
    
    canShoot = false
    canSwitch = false
    canReload = false
    isCharging = true
    
    chargeEvent:FireServer(true)
    
    for i,v in pairs(framework.vm:GetDescendants()) do
        if v:IsA("BasePart") then
            v:SetAttribute("OldTransparency", v.Transparency)
            v.Transparency = 0.999999
        end
    end
    
    autoRunning = run.RenderStepped:Connect(function(dt)
        hum:Move(Vector3.new(0,0,-1), true)
    end)
    
    print("Waiting for 4.5 seconds...")  -- Debug print statement
    wait(4.5)
    
    print("Deactivating ability...")  -- Debug print statement
        
    isCharging = false
    autoRun = false
    canShoot = true
    canSwitch = true
    canReload = true
    autoRunning:Disconnect()
    chargeEvent:FireServer(false)
    
    isAbilityActive = false
end
1 Like

Alright, it works now, thank you

1 Like

sure! not problem, you’re welcome good luck! :smiley: :smiley:

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