local FLY_SPEED = 50
local TOGGLE_KEY = "f"
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local isFlying = false
local camera = workspace.CurrentCamera
local attachment = nil
local linearVelocity = nil
local alignOrientation = nil
local runConnection = nil
local function startFlying()
if not humanoidRootPart or not humanoid then return end
humanoid:ChangeState(Enum.HumanoidStateType.Physics)
attachment = Instance.new("Attachment")
attachment.Name = "FlyAttachment"
attachment.Parent = humanoidRootPart
linearVelocity = Instance.new("LinearVelocity")
linearVelocity.Name = "FlyVelocity"
linearVelocity.Attachment0 = attachment
linearVelocity.MaxForce = math.huge
linearVelocity.VectorVelocity = Vector3.new(0, 0, 0)
linearVelocity.RelativeTo = Enum.ActuatorRelativeTo.World
linearVelocity.Parent = humanoidRootPart
alignOrientation = Instance.new("AlignOrientation")
alignOrientation.Name = "FlyOrientation"
alignOrientation.Attachment0 = attachment
alignOrientation.MaxTorque = math.huge
alignOrientation.Responsiveness = 20
alignOrientation.Mode = Enum.OrientationMode.OneAttachment
alignOrientation.CFrame = humanoidRootPart.CFrame
alignOrientation.Parent = humanoidRootPart
runConnection = RunService.RenderStepped:Connect(function()
local moveDirection = Vector3.new(0, 0, 0)
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
moveDirection = moveDirection + camera.CFrame.LookVector
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
moveDirection = moveDirection - camera.CFrame.LookVector
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
moveDirection = moveDirection - camera.CFrame.RightVector
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
moveDirection = moveDirection + camera.CFrame.RightVector
end
if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
moveDirection = moveDirection + Vector3.new(0, 1, 0)
end
if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
moveDirection = moveDirection - Vector3.new(0, 1, 0)
end
if moveDirection.Magnitude > 0 then
linearVelocity.VectorVelocity = moveDirection.Unit * FLY_SPEED
else
linearVelocity.VectorVelocity = Vector3.new(0, 0, 0)
end
alignOrientation.CFrame = camera.CFrame
end)
end
local function stopFlying()
isFlying = false
if runConnection then
runConnection:Disconnect()
runConnection = nil
end
if linearVelocity then linearVelocity:Destroy() end
if alignOrientation then alignOrientation:Destroy() end
if attachment then attachment:Destroy() end
if humanoid then
humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
end
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode[TOGGLE_KEY:upper()] then
isFlying = not isFlying
if isFlying then
startFlying()
else
stopFlying()
end
end
end)
localPlayer.CharacterAdded:Connect(function(newCharacter)
stopFlying()
character = newCharacter
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoid = character:WaitForChild("Humanoid")
end)