How do i make a flying script?

So im trying to make a flying system for my game, but i have absolutley no idea how. So what im trying to achieve is when you hold p you will fly upwards. Here is my attempt so far.

local p = game.Players.LocalPlayer
local bo = game:GetService(“UserInputService”)
local hrp = p.Character:WaitForChild(“HumanoidRootPart”)
local t = function()
game.Players:WaitForChild(“Humanoid”).Character.CFrame = CFrame.new(0,10,0)
end

bo.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.P then
function t()
end
end
end
end)

I know it is very illogical and stuff, but im a beginner. Also i would need everyone it to be on the server. By the way i will be using a ramiel morph that i made and i believe that will interfere with it. Any help?

3 Likes

This wouldnt work the way you want it to. When you click P it will just teleport you a bit upwards and fall down beacause t() is only called everytime you press P not every frame while holding P. I suggest you watch a Youtube tutorial on how to do it properly. It will save you so much time.

1 Like

@supergeorge2007’s post explains the problem, the solution is turning the P key into a flag that enables and disables when you press and release P, and then fly every renderstepped depending on if the P flag is true or false. Also I suggest you use space instead and also reset the player’s velocity when going up.

Edit: ok a big problem is that you need to do game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame *= CFrame.new(Vector3.new(0, 10, 0)) and not game.Players:WaitForChild(“Humanoid”).Character.CFrame = CFrame.new(0,10,0).

Fixed code:

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
Player.CharacterAdded:Wait()
local hrp = Player.Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local KeyFlags = {
P = false
}

function move(delta)
	Player.Character:WaitForChild("HumanoidRootPart").CFrame *= CFrame.new(Vector3.new(0, 10*delta, 0))
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.P then
			move(1)
		end
	end
end)

RunService.RenderStepped:Connect(function(delta)
	--your code here
end)

I have made variables more readable and fixed the bad line of code but you’ll have to setup the flag and insert the renderstepped delta yourself.

Edit: also if you just want infinite jump don’t go through this trouble, just hack the jump state lol it’s way easier

game:GetService("RunService").RenderStepped:Connect(function()
	if not game.Players.LocalPlayer.Character then return end
	if not game.Players.LocalPlayer.Character.Humanoid then return end

	if game.Players.LocalPlayer.Character.Humanoid.Jump then
		game.Players.LocalPlayer.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end)
2 Likes

Great code! But the thing i dont understand is what the code at the bottom (where i am supposed to put my own code in) does or how it gets activated. Sorry for not replying for a day, i needed sleep.

The code at the bottom runs every frame. If you intend to make your fly work smoothly you should put your movement code/update function there. Also, delta tells how long it took to render the frame, so you should use it if you want it to work at all framerates.

I tried using your code (this one " Player.Character:WaitForChild(“HumanoidRootPart”).CFrame = CFrame.new(Vector3.new(0, 10delta, 0))") in it. It didnt do anything. What can i put in it to make it work?

Whoops I made a typo it was supposed to be game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame *= CFrame.new(Vector3.new(0, 10*delta, 0))

Edit: wait a second I didn’t make a typo but then how did u break the working code so much?? Omg well still the new code should be working.

Edit 2: wait the Player.Character is fine in the new code, the code is fine, but have you tried this button?
image

I have now used that buttoning button.

This is how the code looks for me:

local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
Player.CharacterAdded:Wait()
local hrp = Player.Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local KeyFlags = {
	P = false
}

function move(delta)
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame *= CFrame.new(Vector3.new(0, 10*delta, 0))
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.P then
			move(1)
		end
	end
end)

RunService.RenderStepped:Connect(function(delta)
	game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame *= CFrame.new(Vector3.new(0, 10*delta, 0))
end)

Right now you’d just go up every frame. You need to add a condition to control when you fly.

But im not going up at all?
i am o

Wait whaat?! Wait a sec does pressing P work?

nope. nothing at all.
does it matter if im on windows 10

the script is in startergui btw

I’m on Windows 11 and it worked fine so no it doesn’t matter that you’re on Windows 10. Also, is it a LocalScript? If it is, then try resetting before you use the fly and see if that works. Different places may have different timings.

it is a localscript. I tried resetting but uhh. no result

Try putting it in StarterPlayerScripts.

It worked! Thanks a lot! But now all that is left is making it so that when i hold it it keeps repeating it, and me being able to levitate mid air automatically.

As I said, from here you can add an if statement around the fly code at the bottom renderstepped and turn the inputbegan into a flag changer. Good luck!

Thanks! I am satisfied with the results now.

1 Like