Double jump script not working

I tried to make a double jump script but it did not work this is the script and the error on output

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local uis = game:GetService("UserInputService")

local canDoubleJump = false
local hasLanded = true

humanoid.StateChanged:Connect(function(previous, new)
	
	if new == Enum.HumanoidStateType.Jumping and hasLanded then
		
		if not canDoubleJump then canDoubleJump = true; hasLanded = false end
		
	elseif new == Enum.HumanoidStateType.Landed then
		
		canDoubleJump = false
		hasLanded = true
	end
end)

uis.InputBegan:Connect(function(input, processed)
	
	if processed then return end
	
	if input.Keycode == Enum.KeyCode.Space then
		if canDoubleJump then
			
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			canDoubleJump = false
		end
	end
end)

This is the error
image

2 Likes
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()

uhm I should I add this or replace some line with this?

You put the script in the wrong place. Move it to StarterCharacterScripts. Not StarterPlayerScripts

Reason being;

3 Likes
local players = game:GetService("Players")
local player = players.Player or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local canDoubleJump = false
local hasLanded = true

humanoid.StateChanged:Connect(function(previous, new)
	if new == Enum.HumanoidStateType.Jumping and hasLanded then
		if not canDoubleJump then canDoubleJump = true; hasLanded = false end
	elseif new == Enum.HumanoidStateType.Landed then
		canDoubleJump = false
		hasLanded = true
	end
end)

uis.InputBegan:Connect(function(input, processed
	if processed then return end
	if input.Keycode == Enum.KeyCode.Space then
		if canDoubleJump then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			canDoubleJump = false
		end
	end
end)

Local script, place in a suitable location.

Replace the line. Replace the line.

Nope this did not work either any other solutions and what could be the problem

replace Keycode to KeyCode

then, put the script inside StarterPlayer → StarterCharacterScripts

ok you mean the variable right because the enum one is capital itself

Nope I tried that it does not seem to work

no. the left one
input.Keycode
change to
input.KeyCode

Yep I changed that itself

Jump request are actually already bound.
I believe getting rid of this

if processed then return end

Will fix your problem. Since the all taps of the spacebar are counted as jump requested they are always processed. (unless you unbind them or give the input a higher priority)

Alternatively, you can just copy the code from this tutorial. If you follow through you will see that they used a different event too.