Help with jump script

Ok, so I’m trying to make a jump script where the jump height depends on how long you hold space.
But there are 2 issues. If you spam space while in air, you kind of glide in the air and I don’t want the inputended function to run if you are falling because it looks unnatural.

How can I achieve this?


local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid

local power = 0
local keyDown = false

UserInputService.InputBegan:Connect(function(key)
	if (key.KeyCode == Enum.KeyCode.Space) then
		
        keyDown = true -- set keydown to true
        power = 0 -- set power back to 0 

        while (keyDown) do -- while keydown
              wait()
              power = power + 1-- increase power
        end
    end
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.Space then
		keyDown = false
		character.HumanoidRootPart.Velocity = Vector3.new(		 
            character.HumanoidRootPart.Velocity.X,
		    character.HumanoidRootPart.CFrame.UpVector * power, -- and boom, jumps as high as you held the key down for
		    character.HumanoidRootPart.Velocity.Z
        )
	end

end)

1 Like

Put
if humanoid:GetState(“Freefall”) then return end
right after inputended

You can keep track of when the player touches the ground again by using HumanoidStateType.

Basically, you never track if the player is in the air or other states. For example:

local Humanoid -- Make sure you have the humanoid
local InAir = false

Humanoid.StateChanged:Connect(function(OldState, NewState)
    if NewState == Enum.HumanoidStateType.Freefall then
        InAir = true
    elseif OldState == Enum.HumanoidStateType.Freefall and NewState == Enum.HumanoidStateType.Landed then
        InAir = false
    end
end)

Then, from there, you can just check if they are in the air or not, and if they are, then don’t do anything.

I hope this helps!

lol I was making some script to help you, what do you need to do seems easy but it is not at all, man.

yea lol i just gave up i’m sticking with the basic jump for now ig

1 Like