Help on flying script?

Why I cant move while flying I just stuck and cant move foward or backward ,can you help me and thanks
Capture
this is the local script :

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local char = player.Character or player.CharacterAdded:Wait()

wait(1)

local IsFlying = false
local Torso = char.UpperTorso
local last = tick()

local function flight()
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bv.Parent = Torso
	bv.Name = "FlightVelocity"
	bv.Velocity = Vector3.new(0,0,0)
end

UIS.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	
	if input.KeyCode == Enum.KeyCode.Space then
		if (tick()-last) < 0.5 then
			IsFlying = not IsFlying
			if IsFlying then
				flight()
			else
				local bv = Torso:FindFirstChild("FlightVelocity")
				if bv then bv:Destroy() end
			end
		elseif IsFlying then
			local bv = Torso:FindFirstChild("FlightVelocity")
			if bv then bv.Velocity = Vector3.new(0,16,0) end
		end
		
		last = tick()
	end
	if input.KeyCode == Enum.KeyCode.LeftControl then
		local bv = Torso:FindFirstChild("FlightVelocity")
		if bv then bv.Velocity = Vector3.new(0,-16,0) end
	end
end)

UIS.InputEnded:Connect(function(input,gpe)
	if input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.LeftControl then
		local bv = Torso:FindFirstChild("FlightVelocity")
		if bv then bv.Velocity = Vector3.new(0,0,0) end
	end
end)
3 Likes

Part.Velocity is deprecated. You could try replacing all instances of it with Part.AssemblyLinearVelocity, although I’m not sure if that would fix it. If that doesn’t work, the player’s torso might be anchored.

1 Like

I cant try that now, I am on mobile, but I will try later

1 Like

no Nothing of that is working !?

code for flying script (local)

function fly(parent)
local bodyvelocity = Instance.new("Bodyvelocity")
bodyvelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bodyvelocity.Parent = parent
local flightcontrol = {}
function flightcontrol.Flytodirection(XYORZ,val)
if XYORZ == "X" then
bodyvelocity.Velocity = Vector3.new(val,0,0)
elseif XYORZ == "Y" then
bodyvelocity.Velocity = Vector3.new(0,val,0)
elseif XYORZ == "Z" then
bodyvelocity.Velocity = Vector3.new(0,0,val)
end
end
return flightcontrol

end

However this is just the function for fly, do the input things yourself as I’m lazy to write one

example:

local bodvelocity = fly(player.HumanoidRootPart)
bodvelocity.Flytodirection("Y",10)

Would make him fly up, I like oop and ik I could shorten it but I’m lazy to do that now

1 Like

I will try that if its work I will mark it as solution

1 Like

I also sent example code if your confused

If it dosent work, try setting the bodyvelocities properties

1 Like

Try setting the parent to humanoidrootpart

This is not working :neutral_face: I will try more things

Did you try setting the properties of bodyvelocity and parenting it to humanoid root part? Also did you set the inputbegan stuff?

1 Like

I make input staff but I will retry

Try the updated code, I did some changes

Also can you send your current code?

i will make it just move fowards by pressing z or up arrow

o ye can you send your current code

can you explain more , I think I didnt understand and thanks

Okay, for example you have X,Y,Z, these are the coordinates, so if you want the player to go up (Y) you can do

local bodvelocity = fly(player.HumanoidRootPart)
bodvelocity.Flytodirection("Y",10)

Basically, the first argument in flytodirection is a string (coordinate type) like XYZ, the 2nd argument of flytodirection is the value of the velocity for the specific coordinate type of first argument.

what if the player wants to move between z and x axis ?

x is forward and back, z is left and right., Y I’d up and down

That’s where it gets more complicated since you also have to calculate it with where the character is looking at

1 Like

nvm ,I will try to fix this later

i will try to make cframe calculates