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

How to detect a "short key press"?

Asked by 6 years ago

i want to make it so if you just press a key and release it quickly it does a weak attack but if you hold it down AND then release after a period of time the attack is stronger

i know about inputbegan and inputended but how can i detect a keypress?

so how do i make these functions?


function KeyPress() attack() end function KeyDown() charge() end function KeyUp() attack() end

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

Hello, Bucwheed!

You can use a script like this one:

local PunchCharge = 0

function KeyUp()
    if PunchCharge < 10 then
        print("Not strong: " .. PunchCharge) -- Not too strong attack
    end
    if PunchCharge > 10 and PunchCharge < 100 then
        print("A bit strong: " .. PunchCharge) -- A bit more strong attack
    end
    if PunchCharge > 100 then
        print("Really strong: " .. PunchCharge) -- A really strong attack
    end
    PunchCharge = 0
end


local UIS = game:GetService("UserInputService")

local holdingEKey = false

UIS.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode==Enum.KeyCode.E)then
        holdingEKey = true

        while holdingEKey do
            PunchCharge = PunchCharge + 1
            wait(0.001)
        end

    end
end)

UIS.InputEnded:Connect(function(inputObject)
    if(inputObject.KeyCode==Enum.KeyCode.E)then
        holdingEKey = false
        KeyUp()
    end
end)

-- Don't forget to change values if needed --

Hope this helps!

Good luck with your games!

0
thanks a lot i usually struggle putting these things together myself and this is all i needed to finish a combat system i made. youre the best for putting this together just because i asked. Bucwheed 37 — 6y
Ad

Answer this question