below is a very basic example of how it could be done. though there are some issues with this setup such as the player can immediately restart flying when the movement has been set back to walking. the basic idea used here though is that your player can press F once to begin flying, press F again to exit flying like your current setup, then if they dont leave flight mode withing 3 seconds (delay time) they are automatically taken out of flying.