Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
0
Thanks alot! This on seems to work. littlekit07 16 — 4y
Ad

Answer this question