Determining whether a button input is a press or a hold

I have this script that lets the player fly around in whatever direction they want. Currently, the toggle for flight is mapped to Q, but I would like to make it so that the player has to hold the spacebar.
image

For the purposes of this, we’ll say the arbitrary amount of time they have to hold the spacebar is 0.3 seconds.

Originally, I had the idea of putting a function that runs when the spacebar is pressed (InputBegan) which waits 0.3 seconds and checks to see if the spacebar is being pressed after that time.

However, there is an issue with this plan. A player could double-tap space and flag the flight on accident.

While this works fine in this situation, this is not a universal solution to the problem. If I wanted to assign two different functions depending on whether the player either holds or double-taps, say, the Q button, that would be an issue because they could very easily get mixed up–and it would be an even greater issue on something like a Game Pad where the amount of buttons you have are very limited. This system is not perfect–and I want perfect.

So, I set up a very simple boolean value defaulted to false, which I’ll call spaceNotHeld. When the player lets go of the spacebar, it sets itself to true, waits 0.3 seconds, and sets itself back to false. Then, I make it so that it only runs the function that initiates flight when this boolean value is set to false. This way, if the player lets go of the space bar at any point within 0.3 seconds after pressing it, the value will always be true, and the player will not start flying because they did not hold the space bar.

And that’s how you determine whether an input is a press or a hold!

3 Likes