Disable Jumping While Crouching

I want to disable jumping while my character crouches, but the code I made is not functioning.

Here is the code:

local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Debounce = false
local Crouching = false
local Idle = hum:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("Idle"))
local Move = hum:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("Move"))


UIS.InputBegan:Connect(function(I,S)
	if not S then
		if not Debounce then
			if I.KeyCode == Enum.KeyCode.LeftControl then
				Debounce = true
				Crouching = not Crouching
				wait()
				if Crouching then
					hum.JumpPower = 0
					print("no jump")
					hum.WalkSpeed /= 2 
				else
					hum.JumpPower = 50
					print("jump")
					hum.WalkSpeed *= 2
				end
				wait(0.5)
				Debounce = false
			end
		end
	end
end)

while true do --DONT CHANGE THIS
	wait()
	if Crouching then
		if hum.MoveDirection ~= Vector3.new(0,0,0) then 
			print("Moving")
			Idle:Stop()
			Move:Play()
			
			repeat 
				wait()
			until 
			hum.MoveDirection == Vector3.new(0,0,0) or not Crouching 
		else 
			print("Idle") 
			Idle:Play() 
			Move:Stop() 
			
		end 
	else 
		Idle:Stop() 
		Move:Stop() 
		
	end 
end

Hoping to get some ideas of how I could change my code to get it to function correctly.

Roblox by default uses JumpHeight instead of JumpPower.

You can do either of the two solutions:

  1. Set UseJumpPower to True.
  2. Replace JumpPower with JumpHeight. The JumpHeight by default is 7.5 (Recommended)
1 Like

Another option would be disabling the jumping state of the humanoid using the SetStateEnabled method.

2 Likes

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