How can I fix this?

I made a long jump script, but I’m having an issue were if I continuesly spam the long jump, the state is locked on “LONG_JUMP”.

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

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

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 LockedAnimationSwitch = false

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

local Animator = humanoid:WaitForChild("Animator")

--\\ Animations //--
local function LoadAnimation(id)
	local animation = Instance.new("Animation")
	animation.AnimationId = id

	local animationTrack = Animator:LoadAnimation(animation)
	return animationTrack
end

local Animations = {
	RUNNING = LoadAnimation("rbxassetid://10493878394"),
	WALKING = LoadAnimation("rbxassetid://10494088845"),
	JUMPING = LoadAnimation("rbxassetid://10494212955"),
	FREEFALL = LoadAnimation("rbxassetid://10494451597"),
}

local function ExcludeStopAnimations(animationName)
	for name, animationTrack in pairs(Animations) do
		if not animationTrack.IsPlaying then
			continue
		end

		if name == animationName then
			animationTrack:Play()
		else
			animationTrack:Stop()
		end
	end
end

local runAnim = Animations.RUNNING
local walkAnim = Animations.WALKING
local jumpAnim = Animations.JUMPING
local fallAnim = Animations.FREEFALL


--\\ Functions //--

function speedWatch(speed)
	if speed == 32 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

local function StateCheck()
	local current = humanoid:GetState()
	if current == Enum.HumanoidStateType.Running and humanoid.MoveDirection.Magnitude > 0 then
		if not LockedAnimationSwitch then
			local Speed = humanoid.WalkSpeed
			if Speed > .75 and Speed < 32 then
				state = "WALKING"
				if runAnim.IsPlaying then
					runAnim:Stop()
				end
				if not walkAnim.IsPlaying then
					walkAnim:Play()
				end
				if jumpAnim.IsPlaying then
					jumpAnim:Stop()
				end
				if fallAnim.IsPlaying then
					fallAnim:Stop()
				end
			elseif Speed >= 32 then
				state = "RUNNING"
				if not runAnim.IsPlaying then
					runAnim:Play()
				end
				if walkAnim.IsPlaying then
					walkAnim:Stop()
				end
				if jumpAnim.IsPlaying then
					jumpAnim:Stop()
				end
				if fallAnim.IsPlaying then
					fallAnim:Stop()
				end
			end
		end
	elseif current == Enum.HumanoidStateType.Jumping then
		if not LockedAnimationSwitch then
			LockedAnimationSwitch = true
		
			state = "JUMPING"
			
			if runAnim.IsPlaying then
				runAnim:Stop()
			end
			if walkAnim.IsPlaying then
				walkAnim:Stop()
			end
			if fallAnim.IsPlaying then
				fallAnim:Stop()
			end
			if not jumpAnim.IsPlaying then
				jumpAnim:Play()
			end
			
			task.spawn(function()
				task.wait(.64)
				LockedAnimationSwitch = false
			end)
		end
	elseif current == Enum.HumanoidStateType.Freefall then
		if not LockedAnimationSwitch then
			state = "FREEFALL"
			if runAnim.IsPlaying then
				runAnim:Stop()
			end
			if walkAnim.IsPlaying then
				walkAnim:Stop()
			end
			if jumpAnim.IsPlaying then
				jumpAnim:Stop()
			end
			if not fallAnim.IsPlaying then
				fallAnim:Play()
			end
		end
	else
		if not LockedAnimationSwitch then
			state = "IDLE"
			humanoid.WalkSpeed = 16
			if runAnim.IsPlaying then
				runAnim:Stop()
			end
			if walkAnim.IsPlaying then
				walkAnim:Stop()
			end
			if jumpAnim.IsPlaying then
				jumpAnim:Stop()
			end
			if fallAnim.IsPlaying then
				fallAnim:Stop()
			end
		end
	end
	print(state)
end

local Deb = false

UIS.InputBegan:Connect(function(Input, IsTyping)
	if state == "RUNNING" and state ~= "IDLE" and state ~= "JUMPING" and state ~= "LONG_JUMP" and state ~= "FREEFALL" then
		if IsTyping then return end
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.Space) and humanoid.Running and state ~= "LONG_JUMP" then
			if not Deb then
				LockedAnimationSwitch = true
				Deb = true
				
				for i = 1,20 do
					task.spawn(function()
						task.wait(.0075)
						state = "LONG_JUMP"
					end)
				end
				local speed = Character.Humanoid.WalkSpeed

				local speedBoost = speedWatch(humanoid.WalkSpeed)

				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()
				
				task.wait(.47)
				local state = humanoid:GetState()
				if state == Enum.HumanoidStateType.Freefall then
					root:ApplyImpulse(root.CFrame.LookVector * (400 * speedBoost))
				else
					LockedAnimationSwitch = false
				end
				
				Deb = false
			end
		end
	end
end)

RunService.Heartbeat:Connect(StateCheck)

--\\ Loops //--

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

If I’m not mistaken the problem is in this part

local state = humanoid:GetState()
if state == Enum.HumanoidStateType.Freefall then
	root:ApplyImpulse(root.CFrame.LookVector * (400 * speedBoost))
else
	LockedAnimationSwitch = false
end

when you press the space multiple times the state is changed to freefall and the LockedAnimationSwitch variable never becomes false

a simple solution would be

local state = humanoid:GetState()
if state == Enum.HumanoidStateType.Freefall then
	root:ApplyImpulse(root.CFrame.LookVector * (400 * speedBoost))
end
LockedAnimationSwitch = false
2 Likes