Index Nil error with Variable 'StateChanged'

Hey There, Recently I was trying to develop a script to make my StarterPlayer be able to double jump when the Player clicks his space-bar key twice, but I’ve got a error that it’s declaring that my variable ‘StateChanged’ is a index nil - Line 31.

local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid character:WaitForChild("Humanoid")

local MAX_JUMPS = 2
local TIME_BETWEEN_JUMPS = 0.2
local numJumps = 0
local canJumpAgain = false

local function StateChanged(oldState, newState)
	if Enum.HumanoidStateType.Landed == newState then
		numJumps = 0
		canJumpAgain = false
	elseif Enum.HumanoidStateType.Freefall == newState then
		wait(TIME_BETWEEN_JUMPS)
		canJumpAgain = true
	elseif Enum.HumanoidStateType.Jumping == newState then
		canJumpAgain = false
		numJumps += 1
	end

end

local function onJumpRequest()
	if canJumpAgain and numJumps < MAX_JUMPS then
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end

humanoid.StateChanged:Connect(StateChanged)
UserInputService.JumpRequest:Connect(onJumpRequest)
  1. I want my StarterPlayer Double Jump, Triple Jump, Etc.

  2. I have got a index nil with my variable ‘StateChanged’

  3. I’ve tried to change the variable name to “OnStateChanged”, but still have received the same error.

Please Let me know,

Thanks.

It’s because you forgot an equal sign here.

local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local MAX_JUMPS = 2
local TIME_BETWEEN_JUMPS = 0.2
local numJumps = 0
local canJumpAgain = false

local function StateChanged(oldState, newState)
	if Enum.HumanoidStateType.Landed == newState then
		numJumps = 0
		canJumpAgain = false
	elseif Enum.HumanoidStateType.Freefall == newState then
		wait(TIME_BETWEEN_JUMPS)
		canJumpAgain = true
	elseif Enum.HumanoidStateType.Jumping == newState then
		canJumpAgain = false
		numJumps += 1
	end
end

local function onJumpRequest()
	if canJumpAgain and numJumps < MAX_JUMPS then
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end

humanoid.StateChanged:Connect(StateChanged)
UserInputService.JumpRequest:Connect(onJumpRequest)