I need help with flying script

Okay, so I made a flying script using Zairky’s Script and so I wanted to make a function so that if I didn’t press the key “w” I would stop flying but would still be in the air. So I made the script but every time I try to fly nothing worked.

Script:

local uis = game:GetService(“UserInputService”)
local rs = game:GetService(“RunService”)

local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character
local myHRP = myChar:WaitForChild(“HumanoidRootPart”)
local camera = game.Workspace.CurrentCamera
local LastTapped, Tapped = false, false
local flyUpSpeed = 20
local flyDownSpeed = 20

local toggle = false
local flying = false
local speed = 0.5

local bp = Instance.new(“BodyPosition”, myHRP)
bp.MaxForce = Vector3.new()
bp.D = 10
bp.P = 10000

local bg = Instance.new(“BodyGyro”, myHRP)
bg.MaxTorque = Vector3.new()
bg.D = 10

function fly()
flying = true
bp.MaxForce = Vector3.new(400000,400000,400000)
bg.MaxTorque = Vector3.new(400000,400000,400000)
while flying do
rs.RenderStepped:wait()
bp.Position = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
bg.CFrame = CFrame.new(camera.CFrame.p, myHRP.Position)

uis.InputBegan:connect(function(KeyCode)
if KeyCode == “w” then
if toggle == false then
local bodyGyro = Instance.new (“BodyGyro”)
bodyGyro.MaxTorgue = Vector3.new(0, 0, 0)
bodyGyro.P = (10^6)

				local bodyVel = Instance.new("BodyVelocity")
				bodyVel.MaxForce = Vector3.new(0, 0, 0)
				bodyVel.P = (10^4)
end

end

Could someone tell me what I’m doing wrong?

3 Likes

You forgot to set a parent for the BodyVelocity

1 Like

Ohhhhhh thats why! Thanks mate!

Wait (sorry im just a begginer) how do I set a parent for the BodyVelocity

1 Like

Replace this

local bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(0, 0, 0)
bodyVel.P = (10^4)

with this

local bodyVel = Instance.new("BodyVelocity",MyHRP)
bodyVel.MaxForce = Vector3.new(0, 0, 0)
bodyVel.P = (10^4)
2 Likes

Oh okie thank you once again lol!

Wait also one more thing sorry lol. I have an error saying

11:53:20.448 Workspace.llamagamerzz.CardboardWings.MainFLying:70: Expected ‘end’ (to close ‘function’ at line 37), got ; did you forget to close ‘then’ at line 57? - Studio - MainFLying:70

also here is the entire script

local uis = game:GetService(“UserInputService”)
local rs = game:GetService(“RunService”)

local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character
local myHRP = myChar:WaitForChild(“HumanoidRootPart”)
local camera = game.Workspace.CurrentCamera
local LastTapped, Tapped = false, false
local flyUpSpeed = 20
local flyDownSpeed = 20

local toggle = false
local flying = false
local speed = 0.5

local bp = Instance.new(“BodyPosition”, myHRP)
bp.MaxForce = Vector3.new()
bp.D = 10
bp.P = 10000

local bg = Instance.new(“BodyGyro”, myHRP)
bg.MaxTorque = Vector3.new()
bg.D = 10

function fly()
flying = true
bp.MaxForce = Vector3.new(400000,400000,400000)
bg.MaxTorque = Vector3.new(400000,400000,400000)
while flying do
rs.RenderStepped:wait()
bp.Position = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
bg.CFrame = CFrame.new(camera.CFrame.p, myHRP.Position)

uis.InputBegan:connect(function(KeyCode)
if KeyCode == “w” then
if toggle == false then
local bodyGyro = Instance.new (“BodyGyro”)
bodyGyro.MaxTorgue = Vector3.new(0, 0, 0)
bodyGyro.P = (10^6)

				local bodyVel = Instance.new("BodyVelocity",myHRP)
                bodyVel.MaxForce = Vector3.new(0, 0, 0)
                bodyVel.P = (10^4)
end

end

function endFlying()
bp.MaxForce = Vector3.new(0, 0, 0)
bg.MaxTorque = Vector3.new(0, 0, 0)
flying = false
end

uis.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
if Tapped == false then
Tapped = true
else
LastTapped = true
Tapped = false
if not flying then
fly()
else
endFlying()
end
end
end
end)

Yeah I know its pretty messy

I had to modify this script heavily, so sorry for the wait

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
wait(5)
local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character
local myHRP = myChar:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera
local LastTapped, Tapped = false, false
local flyUpSpeed = 20
local flyDownSpeed = 20
local wdown = false
local toggle = false
local flying = false
local speed = 0.5
local bp = nil
local bg = nil
local bodyVel = nil



function fly()
	bp = Instance.new("BodyPosition",myHRP)
	bp.MaxForce = Vector3.new()
	bp.D = 100
	bg = Instance.new("BodyGyro",myHRP)
	bg.MaxTorque = Vector3.new()
	bg.D = 100
	flying = true
	bp.Position = myHRP.Position + Vector3.new(0,10,0)
	bp.MaxForce = Vector3.new(400000,400000,400000)
	bg.MaxTorque = Vector3.new(400000,400000,400000)
end
while flying do
	rs.RenderStepped:wait()
	bp.Position = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
	bg.CFrame = CFrame.new(camera.CFrame.p, myHRP.Position)
end
uis.InputBegan:connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W and flying == true and wdown == false then
		if toggle == false then
			bp:Destroy()
			wdown = true
			bodyVel = Instance.new("BodyVelocity",myHRP)
			bodyVel.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			while flying and wait() do
				bodyVel.Velocity = camera.CFrame.LookVector*speed*100
			end
		end

	end
end)

uis.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W and flying == true and wdown == true then
		if toggle == false then
			wdown = false
			bodyVel:Destroy()
			bp = Instance.new("BodyPosition",myHRP)
			bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bp.D = 100
			bp.Position = myHRP.Position
		end
	end
end)

function endFlying()
	bp:Destroy()
	bg:Destroy()
	flying = false
end


uis.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space then
		if Tapped == false then
			Tapped = true
		else
			LastTapped = true
			Tapped = false
			if not flying then
				fly()
			else
				endFlying()
			end
		end
	end
end)
3 Likes

Oh its okay! And thank u so much for helping me!!!

oh yeah there’s a wait(5) at the start, just make sure not to make that wait() too fast or the character won’t be found

Oh ye btw sometimes when I try to fly again, I can’t really control my character when I’m flying. It just flies in one direction. Do you know why?

The direction you fly is relative to the camera