How do i make hold a space key to jump higher

im confused by other articles and i dont know where to start with

i wanted to use jumpRequest
but then i realized that jump request fires multiple time

and i still cannot understand what is UIS.IsKeyDown and how do i use it with humanoid.Jumping

1 Like

Here’s a code sample on how to do that
You’ll need 2 scripts:
a Local script under starter character script:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local chargeJumpEvent = ReplicatedStorage:WaitForChild("ChargeJumpEvent")
local player = game.Players.LocalPlayer
local isCharging = false
local chargeStartTime = 0

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    
    if input.KeyCode == Enum.KeyCode.Space then
        isCharging = true
        chargeStartTime = tick()
    end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Space and isCharging then
        isCharging = false
        local chargeTime = tick() - chargeStartTime -- here we calculate how long the player charged 
        chargeJumpEvent:FireServer(chargeTime)
    end
end)

and a server script under server script service:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local chargeJumpEvent = Instance.new("RemoteEvent")
chargeJumpEvent.Name = "ChargeJumpEvent"
chargeJumpEvent.Parent = ReplicatedStorage -- Here the script makes the remote event so you dont have to

local JUMP_POWER_INCREMENT = 50
local MAX_JUMP_POWER = 500

local function onChargeJump(player, chargeTime)
    local character = player.Character
    if character and character:FindFirstChild("Humanoid") then
        local humanoid = character:FindFirstChild("Humanoid")
        local originalJumpPower = humanoid.JumpPower
        local newJumpPower = math.min(originalJumpPower + chargeTime * JUMP_POWER_INCREMENT, MAX_JUMP_POWER)
        humanoid.JumpPower = newJumpPower
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
        wait(0.1) -- Small delay to ensure jump is executed
        humanoid.JumpPower = originalJumpPower -- Reset to original jump power
    end
end

chargeJumpEvent.OnServerEvent:Connect(onChargeJump)
3 Likes

it doesnt seems like its working properly

i hold the key but maximum number it reaching is 139ish in output but in game its still 50

2 Likes

but it gave me a starting point for me

still thank even if script doesnt really work as intentionally

2 Likes

ok so mb i just wrote the script without really testing
but this new one does the job, no need for server:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local chr = player.Character or player.CharacterAdded:Wait()
local hum :Humanoid = chr:WaitForChild("Humanoid")
local isCharging = false
local chargeStartTime = 0

local JUMP_POWER_INCREMENT = 50
local MAX_JUMP_POWER = 500
hum.JumpHeight = 0 -- Set jump height to 0 at the beginning
hum.Jump = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.Space then
		isCharging = true
		chargeStartTime = tick()
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Space and isCharging and hum:GetState() ~= Enum.HumanoidStateType.Freefall then
		isCharging = false
		local chargeTime = tick() - chargeStartTime
		local originalJumpPower = hum.JumpHeight
		local newJumpPower = math.min(originalJumpPower + chargeTime * JUMP_POWER_INCREMENT, MAX_JUMP_POWER)
		hum.JumpHeight = newJumpPower
		
		hum:ChangeState(Enum.HumanoidStateType.Jumping)
		task.wait() -- necessary slight delay
		hum.JumpHeight = 0 -- reset the jump height
	end
end)

if it worked, i’d love it if you solution this :smiley:

2 Likes

I know this is solved, but it seems to me like you are wanting not a charged jump, but to jump higher as you hold the space key. I would recommend the use of forces to achieve this without relying on changing the Humanoid properties.

I achieved this effect through the use of VectorForce to apply while the person jumped and is in the air and the space key is being held down. It releases after letting go of space, after half a second, or after landing.

This is a LocalScript inside StarterPlayerScripts.

--!strict

local EXTRA_FORCE: number = 15
local DESTROY_FORCE_AFTER: number = 0.5

local UserInputService = game:GetService("UserInputService")

local Character: Model = script.Parent
local Humanoid: Humanoid = Character:WaitForChild("Humanoid") :: Humanoid
local HumanoidRootPart: BasePart = Character:WaitForChild("HumanoidRootPart") :: BasePart
local RootAttachment: Attachment = HumanoidRootPart:WaitForChild("RootAttachment") :: Attachment

local delayedJumpThread: thread? = nil
local inputEndedConnection: RBXScriptConnection? = nil
local vectorForce: VectorForce? = nil

local function release(): ()
	if delayedJumpThread ~= nil then
		task.cancel(delayedJumpThread)
		delayedJumpThread = nil
	end

	if inputEndedConnection ~= nil then
		inputEndedConnection:Disconnect()
		inputEndedConnection = nil
	end
	
	if vectorForce ~= nil then
		vectorForce:Destroy()
		vectorForce = nil
	end
end

Humanoid.StateChanged:Connect(function(_: Enum.HumanoidStateType, newState: Enum.HumanoidStateType): ()
	if newState == Enum.HumanoidStateType.Jumping and UserInputService:IsKeyDown(Enum.KeyCode.Space) == true then
		if inputEndedConnection == nil then
			inputEndedConnection = UserInputService.InputEnded:Connect(function(inputObject: InputObject): ()
				if inputObject.KeyCode == Enum.KeyCode.Space then
					release()
				end
			end)
		end
		
		if delayedJumpThread == nil then
			if vectorForce == nil then
				vectorForce = Instance.new("VectorForce")
				assert(vectorForce ~= nil)
				
				vectorForce.Name = "JumpVectorForce"
				vectorForce.Attachment0 = RootAttachment
				vectorForce.ApplyAtCenterOfMass = true
				vectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
				vectorForce.Force = Vector3.new(0, Humanoid.JumpHeight * EXTRA_FORCE * HumanoidRootPart.AssemblyMass, 0)
				vectorForce.Parent = HumanoidRootPart
			end
			
			delayedJumpThread = task.delay(DESTROY_FORCE_AFTER, function(): ()
				delayedJumpThread = nil
				release()
			end)
		end
	elseif (newState == Enum.HumanoidStateType.Landed or newState == Enum.HumanoidStateType.Running) then
		release()
	end
end)
2 Likes

i actually wanted both version

still thanks for help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.