Can't jump on fly script?

Alright So I made a jump script so when I press space twice (double jump) I couldn’t jump! But not always cannot jump. Sometimes I cannot jump I don’t know what I have done wrong. I mean I could jump but its like something is blocking my head from jumping. But nothing is blocking it. Sometimes even on one jump I would fly instantly.

THE FULL FLY CODE:

local camera = game.Workspace.CurrentCamera;

local character = game.Players.LocalPlayer.CharacterAdded:Wait();
local hrp = character:WaitForChild("HumanoidRootPart");
local humanoid = character:WaitForChild("Humanoid");
local animate = character:WaitForChild("Animate");

while (not character.Parent) do character.AncestryChanged:Wait(); end
local idleAnim = humanoid:LoadAnimation(script:WaitForChild("IdleAnim"));
local moveAnim = humanoid:LoadAnimation(script:WaitForChild("MoveAnim"));
local lastAnim = idleAnim;

local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;

local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;

local isFlying = false;
local isJumping = false;
local movement = {forward = 0, backward = 0, right = 0, left = 0};

-- functions

local function setFlying(flying)
	isFlying = flying;
	bodyGyro.Parent = isFlying and hrp or nil;
	bodyVel.Parent = isFlying and hrp or nil;
	bodyGyro.CFrame = hrp.CFrame;
	bodyVel.Velocity = Vector3.new();
	
	animate.Disabled = isFlying;
	
	if (isFlying) then
		lastAnim = idleAnim;
		lastAnim:Play();
	else
		lastAnim:Stop();
	end
end

local function onUpdate(dt)
	if (isFlying) then
		local cf = camera.CFrame;
		local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.forward - movement.backward);
		
		if (direction:Dot(direction) > 0) then
			direction = direction.unit;
		end

		bodyGyro.CFrame = cf;
		bodyVel.Velocity = direction * humanoid.WalkSpeed * 3;
	end
end

local function onJumpRequest()
	if (not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
		return;
	end
	
	if (isFlying) then
		setFlying(false);
		isJumping = true;
	elseif (isJumping) then
		setFlying(true);
	end
end
	
local function onStateChange(old, new)
	if (new == Enum.HumanoidStateType.Landed) then	
		isJumping = false;
	elseif (new == Enum.HumanoidStateType.Jumping) then
		isJumping = true;
	end
end

local function movementBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		movement[actionName] = 1;
	elseif (inputState == Enum.UserInputState.End) then
		movement[actionName] = 0;
	end
	
	if (isFlying) then
		local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0;
		local nextAnim = isMoving and moveAnim or idleAnim;
		if (nextAnim ~= lastAnim) then
			lastAnim:Stop();
			lastAnim = nextAnim;
			lastAnim:Play();
		end
	end
	
	return Enum.ContextActionResult.Pass;
end

-- connections

humanoid.StateChanged:Connect(onStateChange);
game:GetService("UserInputService").JumpRequest:Connect(onJumpRequest);

game:GetService("ContextActionService"):BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward);
game:GetService("ContextActionService"):BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward);
game:GetService("ContextActionService"):BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
game:GetService("ContextActionService"):BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);

game:GetService("RunService").RenderStepped:Connect(onUpdate)

I don’t know what am I doing wrong in the code. I have done everything right. I am with the double jump detection and everything. The problem is, It won’t let me jump at random for some reasons and I don’t know why.

Know any ideas? Reply below. :slight_smile:

2 Likes

It’s probably because of this line

because since you are firing the fly script when it’s supposed to jump, technically you are “disabling” the jumping.
To fix this try using another keybind

1 Like

You could probably connect it to a script that ticks the count so whenever the player’s jump count ticks to 2+ they start flying. That’s what I did with my code. That or if they jump when not grounded. You can check that too.

1 Like

This is not the cause of the issue. JumpRequest is just an event which is always fired on the jump request. Connecting a function to it won’t make it prevent jumping.

1 Like

As I see here you set isJumping to true twice. Could you try to remove the line

isJumping = true

located in the onJumpRequest function?

2 Likes

Thanks so much! I can now finally jump properly without problems :grin:

1 Like