How to add a cooldown

I made a night vision goggle, but I would like to understand and figure out how i can apply a cooldown to it, so that they cannot just keep spamming it on and off.

LocalScript Code

local key = game:GetService("UserInputService")
local event1 = game.ReplicatedStorage.NightGogglesOn
local event2 = game.ReplicatedStorage.NightGogglesOff
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local bool = player.Goggles
key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and bool.Value == false  then
		event1:FireServer(player)
		
		
	elseif input.KeyCode == Enum.KeyCode.E and bool.Value == true then
		event2:FireServer(player)
		

		
	end
end)
event1.OnClientEvent:Connect(function()

	game.Lighting.Atmosphere.Density = 0.75
	game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(112, 255, 61)

end)


event2.OnClientEvent:Connect(function()



	game.Lighting.Atmosphere.Density = 1
	game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(50,50,50)

end)

ServerScript Code

local event1 = game.ReplicatedStorage.NightGogglesOn
local event2 = game.ReplicatedStorage.NightGogglesOff


event1.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local bool = player.Goggles
	local onGoggles = character:WaitForChild("Handle")
	local offGoggles = character:WaitForChild("HandleOff")
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local goggle = character:WaitForChild("Handle")
	
	-- Create a new "Animation" instance and assign an animation asset ID
	local gogglesOn = Instance.new("Animation")
	gogglesOn.AnimationId = "rbxassetid://14287606700"

	-- Load the animation onto the animator
	local gogglesOn = animator:LoadAnimation(gogglesOn)
	
	-- Play the animation track
	gogglesOn:Play()
	
	bool.Value = true
	gogglesOn:GetMarkerReachedSignal("GoggleSwitch"):Connect(function(paramString)
		onGoggles.Transparency = 0
		offGoggles.Transparency = 1
	end)
	gogglesOn.Stopped:Wait()

	event1:FireClient(player)
	---game.Lighting.Atmosphere.Density = 0.75
	---game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(112, 255, 61)

	
end)

event2.OnServerEvent:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local bool = player.Goggles
	local onGoggles = character:WaitForChild("Handle")
	local offGoggles = character:WaitForChild("HandleOff")
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	
	-- Create a new "Animation" instance and assign an animation asset ID
	local gogglesOff = Instance.new("Animation")
	gogglesOff.AnimationId = "rbxassetid://14287453802"

	-- Load the animation onto the animator
	local gogglesOff = animator:LoadAnimation(gogglesOff)
		
	-- Play the animation track
	gogglesOff:Play()

	gogglesOff:GetMarkerReachedSignal("GogglesOff"):Connect(function(paramString)
		onGoggles.Transparency = 1
		offGoggles.Transparency = 0
	end)
	bool.Value = false
	
	
	gogglesOff.Stopped:Wait()
	
	event2:FireClient(player)
	--game.Lighting.Atmosphere.Density = 1
	--game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(50,50,50)
	


end)

You can add a debounce system into the local script like this:

local key = game:GetService("UserInputService")
local event1 = game.ReplicatedStorage.NightGogglesOn
local event2 = game.ReplicatedStorage.NightGogglesOff
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local bool = player.Goggles
local db = false
local dbtime = 1
key.InputBegan:Connect(function(input)
	if not db then
		db = true
		if input.KeyCode == Enum.KeyCode.E and bool.Value == false  then
			event1:FireServer(player)
		
		elseif input.KeyCode == Enum.KeyCode.E and bool.Value == true then
			event2:FireServer(player)

		end
		task.wait(dbtime)
		db = false
	end
end)
event1.OnClientEvent:Connect(function()

	game.Lighting.Atmosphere.Density = 0.75
	game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(112, 255, 61)

end)


event2.OnClientEvent:Connect(function()



	game.Lighting.Atmosphere.Density = 1
	game.Lighting.ColorCorrection.TintColor = Color3.fromRGB(50,50,50)

end)
1 Like
local cooldown = false

key.InputBegan:Connect(function(input)
	if cooldown == false then
		cooldown = true
		if input.KeyCode == Enum.KeyCode.E and bool.Value == false then
			event1:FireServer(player)
		elseif input.KeyCode == Enum.KeyCode.E and bool.Value == true then
			event2:FireServer(player)
		end
		wait(2)
		cooldown = false
	end
end)

Hopefully this should do the trick, sorry for any syntax errors its late at night when im typing this

insert this in your client script on the InputBegan part

1 Like

dangggggg beat me to it, shouldve typed faster lol.

1 Like

Thanks man, Iā€™m still not proficient at coding so thanks for the code too.

Thanks man, and its fine if there are any.

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