How can I get my player out the air and be normal

Im working on a script that puts me in the air if I click my mouse and when I hold the mouse button down I can move around in the air and when I let go it stops.

The problem is when I release the button too early I just stay in the air and I’m just stuck there

I tried doing else if and other measures to make it not do it but it doesn’t work and even did true or false statements just can’t get it to work.

local tool = script.Parent
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local player = game:GetService("Players").LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()

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

--Variables--
local Enabled = true
local ButtonDown = false
local Key = nil
local Rise = nil
local Usable = false

--------------------------------
tool.Equipped:Connect(function()
	Usable = true
end)

tool.Unequipped:Connect(function()
	Usable = false
end)
--------------------------------
UIS.InputBegan:Connect(function(input,gp)

	if gp then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Usable == true then
		local Jump = Character.HumanoidRootPart.position + Vector3.new(0,20,0)
		
		Rise = true
		
		local BP = Instance.new("BodyPosition", Character.HumanoidRootPart)
		BP.Position = Jump
		BP.MaxForce = Vector3.one * 999999
		BP.D = 200
		BP.P = 800
		

		if Jump == Jump  then
			task.wait()
			Rise = false
			end
		if Rise == false then
				BP:Destroy()
			
		end
	end
end)


UIS.InputBegan:Connect(function(input,gp)
	if gp then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Usable == true and Enabled == true then
		task.wait(0.5)
	ButtonDown = true
		Key = "Click"
		while ButtonDown == true do
			local Position = mouse.Hit.Position
			Character.HumanoidRootPart.Anchored = true 
			Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Position)
			RS.Heartbeat:Wait()
		end
			task.wait()
			player.Character.HumanoidRootPart.Anchored = false
		Enabled = true
	
	end
end)

UIS.InputEnded:Connect(function(input, gp)
	if gp then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Usable == true and Key == "Click" then
	
		ButtonDown = false
		Key = nil
		Rise = nil
	
	end
end)
1 Like

(This answer may not work as i expect, cause of my limited time to answer your question)

But if it bugs when you let go too quickly, you can try making a loop that will constantly detect if you’re still holding the button.

This means that inputended won’t run once but it will run multiple times, therefore it should fix itself.

Why have you got 2 inputbegan?
Also, you having a task.wait(0.5) when you click MouseButton1 might be a cause.

1 Like

For the loop, the while buttondown is the loop that checks if the button is still down and if the other UIS that’s how I stop it when the button is no longer pressed.

For the task wait I need to put that there so that and go in the air without it they would both run and I won’t put myself in the air.

I’ve also thought about seeing if I can have something that checks it but my methods don’t work.

1 Like