How do I fix this Long Jump script?

Hello, Im trying to make a long jump script, but Im having an issue where the state only changes after I jump. How would I fix this?

--\\ Variables //--
repeat task.wait() until game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Character = game.Players.LocalPlayer.Character
local JumpAnimation = Character.Humanoid:LoadAnimation(RS.LongJumpAnimation.LongJumps)
local DView = 70
local Table = {}

local root = script.Parent.PrimaryPart
local humanoid = root.Parent:WaitForChild("Humanoid")
local state = " "

local stateText = script.Parent:WaitForChild("Head"):FindFirstChildOfClass("BillboardGui").TextLabel

--\\ Functions //--

function speedWatch(speed)
	if speed == 24 then
		local boostVal = 1.25
		return boostVal
	elseif speed == 16 then
		local boostVal = 1.15
		return boostVal
	else
		local boostVal = 1
		return boostVal
	end
end

humanoid.Running:Connect(function(speed)
	if speed == 16 then
		state = "WALKING"
	elseif speed == 0 then
		state = "IDLE"
	else
		if speed == 24 then
			state = "RUNNING"
		end
	end
end)

humanoid.Jumping:Connect(function()
	state = "JUMPING"
end)

local Deb = false

UIS.InputBegan:Connect(function(Input, IsTyping)
	print(state == "RUNNING" and state ~= "IDLE" and state ~= "JUMPING" and state ~= "LONG_JUMP")
	if state == "RUNNING" and state ~= "IDLE" and state ~= "JUMPING" and state ~= "LONG_JUMP" then
		if IsTyping then return end
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.Space) and humanoid.Running then
			if not Deb then
				Deb = true
				state = "LONG_JUMP"
				local speed = Character.Humanoid.WalkSpeed

				local speedBoost = speedWatch(humanoid.WalkSpeed)

				JumpAnimation:Play()

				humanoid.Jump = false humanoid.JumpPower = 0
				root:ApplyImpulse(root.CFrame.LookVector * (1000 * speedBoost))
				workspace.Gravity = 145.8
				root:ApplyImpulse(root.CFrame.YVector * (475 * speedBoost))
				task.wait(0.25)
				workspace.Gravity = 196.2
				humanoid.Jump = false humanoid.JumpPower = 50

				JumpAnimation:Stop()
				
				Deb = false
			end
		end
	end
end)

--\\ Loops //--

while task.wait() do
	stateText.Text = "STATE: "..state
	print(state)
end

2 Likes

Probably its because you are using == instead of <= or >=.

== is used to check if two values are equal.

“<” is used to check if a value is less than another value.

“>” is used to check if a value is greater than another value.

In this case, you want to use <= or >= because you want to check if the value is less than or equal to 23 or greater than or equal to 1.

I hope this helps!

humanoid.Running:Connect(function(speed)
	if speed >= 1 and speed <= 23 then
		state = "WALKING"
	elseif speed == 0 then
		state = "IDLE"
	else
		if speed >= 24 then
			state = "RUNNING"
		end
	end
end)
2 Likes

So I did this, but Im not sure its really working because it seems to usually only change the state to “WALKING” even though I am at the speed of 24.

Unless I jump, my state will switch to anything else but Walking.

humanoid.Running only fires when the player is running, not when jumping or when idle. What I suggest using instead is humanoid.StateChanged which will fire after any player state changes. It would look somewhat like this:

local humanoid : Humanoid = Instance.new('Humanoid')

local runningSpeed = 24

humanoid.StateChanged:Connect(function(state)
	if state == Enum.HumanoidStateType.Running then
		local Speed = humanoid.WalkSpeed
		if Speed > .75 and Speed < runningSpeed then
			print('Walking')
		elseif Speed >= runningSpeed then
			print('Running')
		end
	elseif state == Enum.HumanoidStateType.Jumping then
		print('Jumping')
	end
end)
1 Like

The state never changes, it just prints out a whitespace.

--\\ Variables //--
repeat task.wait() until game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Character = game.Players.LocalPlayer.Character
local JumpAnimation = Character.Humanoid:LoadAnimation(RS.LongJumpAnimation.LongJumps)
local DView = 70
local Table = {}

local root = script.Parent.PrimaryPart
local humanoid = root.Parent:WaitForChild("Humanoid")
local state = " "

local stateText = script.Parent:WaitForChild("Head"):FindFirstChildOfClass("BillboardGui").TextLabel

--\\ Functions //--

function speedWatch(speed)
	if speed == 24 then
		local boostVal = 1.25
		return boostVal
	elseif speed == 16 then
		local boostVal = 1.15
		return boostVal
	else
		local boostVal = 1
		return boostVal
	end
end

humanoid.StateChanged:Connect(function(state)
	if state == Enum.HumanoidStateType.Running then
		local Speed = humanoid.WalkSpeed
		if Speed > .75 and Speed < 24 then
			state = "WALKING"
		elseif Speed >= 24 then
			state = "RUNNING"
		end
	elseif state == Enum.HumanoidStateType.Jumping then
		state = "JUMPING"
	end
end)

local Deb = false

UIS.InputBegan:Connect(function(Input, IsTyping)
	print(state == "RUNNING" and state ~= "IDLE" and state ~= "JUMPING" and state ~= "LONG_JUMP")
	if state == "RUNNING" and state ~= "IDLE" and state ~= "JUMPING" and state ~= "LONG_JUMP" then
		if IsTyping then return end
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.Space) and humanoid.Running then
			if not Deb then
				Deb = true
				state = "LONG_JUMP"
				local speed = Character.Humanoid.WalkSpeed

				local speedBoost = speedWatch(humanoid.WalkSpeed)

				JumpAnimation:Play()

				humanoid.Jump = false humanoid.JumpPower = 0
				root:ApplyImpulse(root.CFrame.LookVector * (1000 * speedBoost))
				workspace.Gravity = 145.8
				root:ApplyImpulse(root.CFrame.YVector * (475 * speedBoost))
				task.wait(0.25)
				workspace.Gravity = 196.2
				humanoid.Jump = false humanoid.JumpPower = 50

				JumpAnimation:Stop()
				
				Deb = false
			end
		end
	end
end)

--\\ Loops //--

while task.wait() do
	stateText.Text = "STATE: "..state
	print(state)
end

Okay, my first response I didn’t think through right lol.

What I think will suit you the best is using RunService and testing the current state every Heartbeat

Something like this might work:

local RunService = game:GetService('RunService')
local state = " "

local function StateCheck()
	local current = humanoid:GetState()
	if current == Enum.HumanoidStateType.Running and humanoid.MoveDirection.Magnitude > 0 then
		local Speed = humanoid.WalkSpeed
		if Speed > .75 and Speed < 24 then
			state = "WALKING"
		elseif Speed >= 24 then
			state = "RUNNING"
		end
	elseif current == Enum.HumanoidStateType.Jumping then
		state = "JUMPING"
	elseif current == Enum.HumanoidStateType.Freefall then
		state = "FREEFALL"
	else
		state = "IDLE"
	end
	print(state)
end
RunService.Heartbeat:Connect(StateCheck)

A couple of things I changed was adding the addition of “humanoid.MoveDirection.Magnitude > 0” which makes sure the player is moving (last script would count “Idle” as"Running")

DM if you have anymore questions