Jump with Crounch

Hello
I need hep with two scripts, where is wrong

  1. I press the SHIFT key and the player crouches
  2. I release the key and the player jumps, according to the length of time the SHIFT key is held

-----------first--------------

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")

local isCrouching = false
local crouchStartTime = 0

local jumpPowerMultiplier = 2.5 -- Úprava síly skoku

local crouchAnimId = "rbxassetid://180436334"
local crouchAnim = Instance.new("Animation")
crouchAnim.AnimationId = crouchAnimId

local loadedAnim = humanoid:LoadAnimation(crouchAnim)
loadedAnim.Looped = false

local function onKeyPress(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isCrouching = true
		crouchStartTime = tick()
		loadedAnim:Play()
	end
end

local function onKeyRelease(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isCrouching = false
		crouchStartTime = 0
		loadedAnim:Stop()
	end
end

local function update()
	if isCrouching then
		local crouchDuration = tick() - crouchStartTime
		local jumpPower = humanoid.JumpPower * (1 + crouchDuration * jumpPowerMultiplier)
		humanoid.JumpPower = jumpPower
	end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)
game:GetService("RunService").Heartbeat:Connect(update)

----------------second------------

local function onCharacterAdded(character)
	character:WaitForChild("Humanoid").StateChanged:Connect(function(oldState, newState)
		if newState == Enum.HumanoidStateType.Jumping then
			local humanoid = character:WaitForChild("Humanoid")
			if humanoid:GetState() == Enum.HumanoidStateType.Crouching then
				humanoid.JumpPower = humanoid.JumpPower / (1 + humanoid:GetStateTime(Enum.HumanoidStateType.Crouching))
			end
		end
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)

Thank you for every one help
Codycheck

local function onKeyRelease(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isCrouching = false
		loadedAnim:Stop()
	end
end

You set crouchStartTime to 0 in your original version so you were essentially doing the tick() minus 0. Perchance.

1 Like

crouching is not a HumanoidStateType, you’d want to handle this entirely on the client pretty much and log via Tick() or Time() how long the player has been holding shift. on release you can either force them to jump via humanoid:SetState(Enum.HumanoidStateType.Jumping) or humanoid.Jump = true and set the jumppower to whatever you want based off the time held, or using a BodyVelocity/LinearVelocity.