Ad
Log in to vote
0

Any help with a cooldown for keybind power?

Asked by 5 days ago
Edited 5 days ago

Not really sure how cooldowns work so I thought a simple wait would work but it doesn't so any help?

local RP = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local HealthDrainDevent = RP:WaitForChild("HealthDrainEvent")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local draining = false
local cooldown = 1


UIS.InputBegan:Connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then 
            local target = mouse.Target

            if target.Parent.Humanoid or target.Parent.Parent.Humanoid then
                local TargetHumanoid = target.Parent.Humanoid or target.Parent.Parent.Humanoid

                if TargetHumanoid ~= player.Character.Humanoid then
                    print("Enemy humanoid detected, initiating doom.")
                    draining = true
                    while draining == true do 
                        HealthDrainDevent:FireServer(TargetHumanoid)
                        wait(cooldown)
                    end
                end
            end
        end 
    end
end)

UIS.InputEnded:Connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E then 
            draining = false
        end
    end
end)
0
There is something on the Output? sebtantin 15 — 5d

1 answer

Log in to vote
0
Answered by 3 days ago

I think you're using debounces the wrong way. This is how they go:

local debounce = false

if not debounce then --checks if debounce is false (would continue only if debounce is false)
    debounce = true --sets debounce to true (so the code will not run again because debounce is currently true)

    --code here

    wait(1) --waits 1 second before setting debounce back to false 
    debounce = false --finally, sets debounce back to false (so the code can now run again)
end

In your case, maybe try this:

UIS.InputBegan:Connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.E and not draining then 
            draining == true

            local target = mouse.Target

            if target.Parent.Humanoid or target.Parent.Parent.Humanoid then
                local TargetHumanoid = target.Parent.Humanoid or target.Parent.Parent.Humanoid

                if TargetHumanoid ~= player.Character.Humanoid then
                    print("Enemy humanoid detected, initiating doom.")

                    HealthDrainDevent:FireServer(TargetHumanoid)
                end
            end

            wait(cooldown)
            draining = false
        end 
    end
end)
Ad

Answer this question