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

Any help with a cooldown for keybind power?

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)

1 comment

0

There is something on the Output?

1 answer

✓ Accepted answer

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)

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