SetStateEnabled doesn't work for my script

I tried using roblox’s uis ‘JumpRequest’ to detect when a player jumps, and add a 2 second cooldown on their jumping. Problem is it doesn’t work the way I thought it would. Anyone know what’s wrong with this script?

local Jumped
function jump()
	if Jumped == false then
		Jumped = true
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		wait(2)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		Jumped = false
	end
end
UIS.JumpRequest:Connect(jump)

Nvm figured it out! My bad guys!

Ok I see the problem, here’s what went wrong:

You created the variable named Jumped. But you did not assign it a value. This means that the value returns nil when referenced.

In your code you have if Jumped == FALSE. However, Jumped is nil, not false. A better way would to have it as:

if not Jumped then

This way, if jumped in nil, then it will still run the inside code.

You could also set jumped to false right from the start. Since I don’t know how you are implementing the script, I will include everything:

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")

local Jumped = false
function jump()
	print("JumpRequest")
	if not Jumped then
		RunService.Stepped:Wait()
		Jumped = true
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		wait(2)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		Jumped = false
	end
end
UIS.JumpRequest:Connect(jump)

Runservice.Stepped:Wait() is in there so it lets you jump before you disable jumping.

I see that you figured it out, but I’ll leave this here just in case.