Forcing a player to jump

I would like help on a script that forces a player to jump when they press “D”

here is my script:

local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,GPE)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.D then
			Humanoid.Jump = true
		end
	end
end)
1 Like

There is no need for that if Statement, other than that it looks fine

1 Like

I’m getting no errors but no response either, but if I add print() then it does print but nothing else.

I just realized I think your doing this in a ServerScript correct? If so, UIS only runs in LocalScripts

2 Likes

it is in a local script now, but the same nothing happens.

thanks for your help by the way

Alright, I figured it out, you need to use the :ChangeState() function, a member function of Humanoid.

Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

But other than this too, you’ll want to bind a sink to the right movement, otherwise the player will move as well (if you weren’t after this and you want the player to jump and move at the same time, then ignore this).

-- We need to bind a sink to the right movement entirely for the user input below to override it.
ContextActionService:BindAction("SinkRightMovement", function() return Enum.ContextActionResult.Sink end, false, rightMovement)

The full script in all should be looking like this:

-- UNCOMMENT THE COMMENTS BELOW IF YOU DON'T WANT THE PLAYER TO MOVE RIGHT

-- We need to bind a sink to the right movement entirely for the user input below to override it.
-- local ContextActionService = game:GetService("ContextActionService")
-- local rightMovement = Enum.PlayerActions.CharacterRight
--ContextActionService:BindAction("SinkRightMovement", function() return Enum.ContextActionResult.Sink end, false, rightMovement)

local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,GPE)
	if input.KeyCode == Enum.KeyCode.D then
		Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end)

Ensure this is in a local script in StarterCharacterScripts, as @domboss37 said, UserInputService can only be ran in local scripts. :slight_smile:

5 Likes

You can force the Characater to jump by changing it’s state.

Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

@xmthl and @nonamehd24, both of your scripts worked but, you can jump mid-air, is there a way this can be fixed? I might be able to work a way around it if it’s too hard to fix.

Setting Jump to true in the Humanoid properties?

You can add a debounce which avoids the :ChangeState from running until a certain amount of time has elapsed, this is how you would do it:

-- UNCOMMENT THE COMMENTS BELOW IF YOU DON'T WANT THE PLAYER TO MOVE RIGHT

-- We need to bind a sink to the right movement entirely for the user input below to override it.
-- local ContextActionService = game:GetService("ContextActionService")
-- local rightMovement = Enum.PlayerActions.CharacterRight
--ContextActionService:BindAction("SinkRightMovement", function() return Enum.ContextActionResult.Sink end, false, rightMovement)

local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local canPlayerJumpYet = true
local jumpDelayTime = 2

UIS.InputBegan:Connect(function(input,GPE)
	if input.KeyCode == Enum.KeyCode.D then
		if canPlayerJumpYet then
			Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			canPlayerJumpYet = false
			wait(jumpDelayTime)
			canPlayerJumpYet = true
		end
	end
end)

What this is essentially doing is, when we press the D key, we check if ‘canPlayerJumpYet’ is true, if it is, we set it to false, so that the code inside this condition cannot be ran until we set it back to true. That’s what the wait(2) is in there for, so when the player jumps, we’re waiting 2 seconds before they can jump again. :slight_smile:

EDIT: I’ve made the jump delay time a variable so you can adjust it to what you need.

5 Likes