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

How to detect a "short key press"?

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

✓ Accepted answer

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!

1 comment

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.

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