Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My keybind script is working but the cooldown won't work. Any ideas?

I am currently trying to make it so there is a cooldown when a player clicks a certain key. I've written out my keybind script, but was wondering how to integrate a cooldown. I've been experimenting but have been unable to fully write out the script.

local debounce = false

local cooldownTime = 1

script.Parent.Equipped:Connect(function(mouse)
	mouse.KeyDown:Connect(function(key)
		if not debounce and key:lower() == "r" or key:upper() == "R" then
			debounce = true
			local reload = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Reload)
			local sound = game:GetService("StarterPack").Pistol.reloading
			reload:Play()
			sound:Play()
			wait(cooldownTime)
			debounce = false
		end 
	end)
end)

This is my latest variation of my original script, though I believe there may be a problem with how I applied the 'and' in my keybind section. I'm relatively new to scripting.

1 answer

✓ Accepted answer

separate equipped from the keydown

local debounce = false
local eq=false
local cooldownTime = 1
local mouse=game.Players.LocalPlayer:GetMouse()
script.Parent.Equipped:Connect(function(mouse)
eq=true
end)
script.Parent.Unequipped:Connect(function(mouse)
eq=false
end)
    mouse.KeyDown:Connect(function(key)
        if not debounce and key== "r" and eq then
            debounce = true
            local reload = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Reload)
            local sound = game:GetService("StarterPack").Pistol.reloading
            reload:Play()
            sound:Play()
            wait(cooldownTime)
            debounce = false
        end 
    end)

1 comment

0

Thanks alot! This on seems to work.

This discussion is preserved as it was. New answers, comments, and votes are closed.