How to make a player fly

how do you actually make the player fly?

It looks like by the works of Body Physics but can’t stress out on how to make it i found a thread weeks ago on how to make a flying system but its client-sided [IGNORE THIS]

so how do i make a flying system for all players and it is server-sided

Also, in UIS how do i make if they press 2 buttons it flies them then presses one button then unfly

Counter Numbers? i guess

if you are willing to give me an example of this i would be very appreciated of your help

EDIT:
I will be back tomorrow to see if there are any responses from this topic!

6 Likes

2 Issues ahead [Options]

These are the 2 Options that i need to fix i have to pick one of them ofcourse and idk how to fix 2 of them or even 1 of them

Server:

If i make this server-sided then it will never fly my character because when i make it fly on the server i use this piece of code

if Humanoid:GetState() ~= Enum.HumanoidStateType.Flying then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Flying, true)
elseif Humanoid:GetState() == Enum.HumanoidStateType.Flying then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, true)
end

but it really doesn’t even fly me into the sky im just still stuck on the ground

Client:
This actually works but in a very weird behavior if i fly it doesn’t actually fly until i move my cam up to the sky and it literally walks me [FORCED] and this code was from 2018 i really expected it to work but it didn’t

here is the topic of whereof i got this code:

Code:

-- requirements
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local counter = 0
local flying = false
local Speed = 1

local bp = Instance.new("BodyPosition", HumanoidRootPart)
bp.MaxForce = Vector3.new()
bp.D = 10
bp.P = 10000

local bg = Instance.new("BodyGyro", HumanoidRootPart)
bg.MaxTorque = Vector3.new()
bg.D = 10

-- fly the char
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 = HumanoidRootPart.Position +((HumanoidRootPart.Position - Camera.CFrame.p).unit * Speed)
		bg.CFrame = CFrame.new(Camera.CFrame.p, HumanoidRootPart.Position)
	end
end

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

If 1 of the sides are fixed im going with that side no more after one side has been fixed

2 Likes

sorry for bumping but if you need any information about this ill be gladly to give you that info

1 Like

You don’t need to bump this. Just edit your original post :grinning:

1 Like

Here is also an video of the weird client-sided bug

as you can see it moves the player but they aren’t pressing anything which is annoying and weird and you can also check the code which is a few posts above like the second reply or the first reply

I figured it out by fixing the BG and BP i found that solution on the other thread and EgoMoose in 2018 linked up an file with a working flying system which i didn’t use before and i did use it now and then it worked and also if you were wondering about the code here it is

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

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local SKILLS_EVENT = game.ReplicatedStorage.Events.Skills

-- requirements
local Character = Player.Character
local Humanoid = Character.Humanoid
local animate = Character:WaitForChild("Animate")
local HumanoidRootPart = Character.HumanoidRootPart
local counter = 0

local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;

local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;

local isFlying = false;
local isJumping = false;
local movement = {forward = 0, backward = 0, right = 0, left = 0};

-- Animations
local idleAnim = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks")["Fly_Idle"])
local moveAnim = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks")["Fly_Move"])
local lastAnim = idleAnim;

local function setFlying(flying)
	isFlying = flying;
	bodyGyro.Parent = isFlying and HumanoidRootPart or nil;
	bodyVel.Parent = isFlying and HumanoidRootPart or nil;
	bodyGyro.CFrame = HumanoidRootPart.CFrame;
	bodyVel.Velocity = Vector3.new();
	
	animate.Disabled = isFlying;
	
	if (isFlying) then
		lastAnim = idleAnim;
		lastAnim:Play();
	else
		lastAnim:Stop();
	end
end

local function onUpdate(dt)
	if (isFlying) then
		local cf = Camera.CFrame;
		local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.forward - movement.backward);
		
		if (direction:Dot(direction) > 0) then
			direction = direction.unit;
		end
		
		bodyGyro.CFrame = cf;
		bodyVel.Velocity = direction * Humanoid.WalkSpeed * 3;
	end
end

local function onJumpRequest()
	if (not Humanoid or Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
		return;
	end
	
	if (isFlying) then
		setFlying(false);
		isJumping = false;
	elseif (isJumping) then
		setFlying(true);
	end
end

local function onStateChange(old, new)
	if (new == Enum.HumanoidStateType.Landed) then	
		isJumping = false;
	elseif (new == Enum.HumanoidStateType.Jumping) then
		isJumping = true;
	end
end

local function movementBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		movement[actionName] = 1;
	elseif (inputState == Enum.UserInputState.End) then
		movement[actionName] = 0;
	end
	
	if (isFlying) then
		local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0;
		local nextAnim = isMoving and moveAnim or idleAnim;
		if (nextAnim ~= lastAnim) then
			lastAnim:Stop();
			lastAnim = nextAnim;
			lastAnim:Play();
		end
	end
	
	return Enum.ContextActionResult.Pass;
end

Humanoid.StateChanged:Connect(onStateChange);
UIS.JumpRequest:Connect(onJumpRequest);

CAS:BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward);
CAS:BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward);
CAS:BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
CAS:BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);

RS.RenderStepped:Connect(onUpdate)

INCLUDES ANIMATION MAKE SURE TO EDIT IT WITH YOUR OWN OBJECTS

Credits to EgoMoose for the code he gave on the place, which i modified to make it compatible with my game

Thanks,
FerbZides

1 Like