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

How would I make a cooldown system for this?

Asked by 5 years ago

So basically on my end, while testing the PC and XBOX controls. The PC controls work perfectly fine but when pushing the button on the other hand the function likes to repeat which has left me confused so I've thought about using a wait() and a while loop that triggers a bool value I just don't know how to execute it.

local player = game.Players.LocalPlayer
local inputServ = game:GetService("UserInputService")
local Key = Enum.KeyCode.E
local Button = Enum.KeyCode.ButtonX


inputServ.InputBegan:connect(function(input)
    if input.KeyCode == Key then
        print("PC Pressed")
        script.Activated:FireServer()
    end
inputServ.InputBegan:connect(function(input)
    if input.KeyCode == Button then
        print("XBOX Pressed")
        script.Activated:FireServer()
    end
end)
end)

1 answer

Log in to vote
0
Answered by
HaveASip 494 Moderation Voter
5 years ago
Edited 5 years ago

You need to use debounce, so here is an example:

local UIS = game:GetService("UserInputService")
local ready = true

UIS.InputBegan:Connect(function(input)
    if ready == true then
        ready = false
        if input.UserInputType == Enum.UserInputType.Keyboard then --Checking if player inputed from keyboard or some other device
            if input.KeyCode == Enum.KeyCode.X then --Checking keycode
                print("Player pressed X using keyboard") --printing
                wait(.5) --Cooldown time
                ready = true
            end
        elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
            if input.KeyCode == Enum.KeyCode.ButtonX then
                print("player pressed X using gamepad")
                wait(.5)
                ready = true
            end
        end
    end
end)
Ad

Answer this question