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)
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.
@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.
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.
EDIT: I’ve made the jump delay time a variable so you can adjust it to what you need.