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

debounce only works once, then player can spam key. what's the issue?

Asked by 4 years ago
Edited 4 years ago

the player presses and holds F, animation and other things go on in the background. then there is a cooldown. after this cooldown the debounce doesn't do it's job any more. how do i fix this?

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

local KeyPressed = false

repeat wait() until Player.Character
c = Player.Character
humanoid = c.Humanoid

local Time = 0
local Debounce = true

--animation codes

    local holdAnim = Instance.new("Animation")
    holdAnim.AnimationId = "http://roblox.com/asset/?id=4896264874"
    local holdTrack = humanoid:LoadAnimation(holdAnim)


    local chargeAnim = Instance.new("Animation")
    chargeAnim.AnimationId = "http://roblox.com/asset/?id=4896212792"
    local chargeTrack = humanoid:LoadAnimation(chargeAnim)

    local releaseAnim = Instance.new("Animation")
    releaseAnim.AnimationId = "http://roblox.com/asset/?id=4896256143"
    local releaseTrack = humanoid:LoadAnimation(releaseAnim)

--function

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if Debounce == false then return end
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = true
        Debounce = false

        local BG = Instance.new("BodyGyro", c.HumanoidRootPart)
        BG.maxTorque = Vector3.new(250000,250000,250000)
        BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)
        BG.P = 20000
        local BP = Instance.new("BodyPosition", c.HumanoidRootPart)
        BP.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        BP.Position = c.HumanoidRootPart.Position

        chargeTrack:Play()

spawn(function()
        while KeyPressed == true and Time < 200 do
            wait()
            BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)    
            Time = Time + 1
            print("Key being held interval: ")
            holdTrack:Play()
            holdTrack.Looped = true
        end
        if KeyPressed == false or Time >= 200 then
        Time = 0
        holdTrack.Looped = false        
        releaseTrack:Play()
        wait(0.5)
        BG:Remove()
        BP:Remove()
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
        print("Key has been let go of")
            end
            wait(5)
            Debounce = true
        end)
    end
end)

2 answers

Log in to vote
1
Answered by 4 years ago

You set the debounce to true outside of your conditional loop, meaning any input would trigger the debounce being set back to true. Simply move it inside of your loop.

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
        print("Key has been let go of") 
         wait(5)
            Debounce = true
            end
            end
        end)
    end
end)
Ad
Log in to vote
0
Answered by 4 years ago
UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
        print("Key has been let go of") 
         wait(5)
            Debounce = false
            end
            end
        end)
    end
end)

try setting debounce to false

Answer this question