Making objects jump due to properties when pressing Space

I want to make it so when the player presses the Space key, their object (not a character) goes up into the air as dictated by a count property. Should the player delay the jump by a few nanoseconds or reach the top of the second jump or fall too fast, it counts as a completed second jump, and the player can jump no higher. I have accomplished most of this, but the object will jump continuously higher each time the spacebar is pressed past the second jump mark. The player should only be allowed to jump twice normally off the ground, and only once in water.

How to accomplish this? I’ve accomplished like 85% of this so far, but the jump cap part for the dynamic non-character object does not work, and I want to make it so when either a “grounded” or “swimming” property is 1, the properties that enable jumping and counting the jumps are reset.

I prefer dynamic objects over characters as you can get linearVelocity and angularVelocity.

I am trying to do this all in Python.

use own.localLinearVelocity[2] = 5.000 # or hight value
assigning linear velocity to an object will not be summed up as if you use applyForce() and the jump in the first iteration will be equal to the second iteration, and the momentum that was added to the object during the first jump will not be taken into account. so that you have a system for calculating the height at which the second jump will work - get the current height of the player plus the height of the jump - as soon as the player’s position is equal to or higher, the second jump is activated
if key_space:
if pos_jump==None: # pos_jump global variabel or property
pos_jump = own.worldPosition.z + 5.0
own.localLinearVelocity[2] = 5.000
if own.worldPosition.z>=pos_jump:
#activate double jump
and don’t forget to reset the variable when landing after a jump - this can be done by checking with a beam or radar