-- Configuration
local FlyToggle = false -- Leave false by default
local FlyKey = "f" -- Change this to toggle your fly with the according key
local Speed = 50 -- The flight speed
local UseCFrame = false -- Determines if moving while flying will be CFrame or Velocity based
-- Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- Variables
local TURNOFFSCRIPT = false -- I don't love wrapping if conditions to everything :(
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local ScreenGui = Instance.new("ScreenGui", PlayerGui)
ScreenGui.Name = "JaysFlyGui123"
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local Root = Char:WaitForChild("HumanoidRootPart")
local Camera = workspace.CurrentCamera
local MobileMoveVec = Vector3.zero
local FlyAnimR6 = 130025294780390
local FlyAnimR15 = 121812367375506
local FlyAnimation = nil
local FlyTrack = nil
-- Movement keys (For flyin)
local MKeys = {
w = false,
a = false,
s = false,
d = false,
q = false,
e = false,
space = false,
}
-- Initialize BV and BAV
local BV = Instance.new("BodyVelocity", Root)
BV.Velocity = Vector3.zero
BV.MaxForce = Vector3.zero
BV.P = 1250
local BAV = Instance.new("BodyAngularVelocity", Root)
BAV.AngularVelocity = Vector3.zero
BAV.MaxTorque = Vector3.zero
BAV.P = 1250
-- Player initializer
local function PlayerInit()
-- Character stuff
Char = Player.Character or Player.CharacterAdded:Wait()
Humanoid = Char:WaitForChild("Humanoid")
Root = Char:WaitForChild("HumanoidRootPart")
-- Gui
ScreenGui = Instance.new("ScreenGui", PlayerGui)
ScreenGui.Name = "JaysFlyGui123"
-- Initialize BV and BAV
BV = Instance.new("BodyVelocity", Root)
BV.Velocity = Vector3.zero
BV.MaxForce = Vector3.zero
BV.P = 1250
BAV = Instance.new("BodyAngularVelocity", Root)
BAV.AngularVelocity = Vector3.zero
BAV.MaxTorque = Vector3.zero
BAV.P = 1250
end
-- Call player initializer
Player.CharacterAdded:Connect(PlayerInit)
-- Seperator
-- Da main-lane vwro B)
-- Change fly speed prompt (Gui stuff)
local function ChangeSpeedPrompt(erm)
local TextBox = Instance.new("TextBox", ScreenGui)
TextBox.PlaceholderText = "Enter Speed"
TextBox.Text = ""
TextBox.Position = UDim2.new(0.5, 0, 0.5, 0)
TextBox.Size = UDim2.new(0.5, 0, 0.5, 0)
TextBox.AnchorPoint = Vector2.new(0.5, 0.5)
TextBox.TextScaled = true
TextBox:CaptureFocus()
TextBox.FocusLost:Connect(function()
if not erm then
if tonumber(TextBox.Text) then
Speed = tonumber(TextBox.Text)
print("Change speed successful! Speed: " .. TextBox.Text)
else
print("Change speed unsuccessful. :( BUT...")
local text = tostring(TextBox.Text):lower()
if text == "key" then
print("Changing key...")
task.delay(0.1, function()ChangeSpeedPrompt(true)end)
elseif text == "cf" then
UseCFrame = not UseCFrame
print("Using CFrame! State:", UseCFrame)
else
print("Nothing will happen :(")
end
end
else
print("Key changed! Key: " .. TextBox.Text:lower())
FlyKey = TextBox.Text:lower()
end
TextBox:Destroy()
end)
end
-- Play fly animation
local function PlayFlyAnimation()
if FlyTrack then FlyTrack:Stop() end
if Humanoid.RigType == Enum.HumanoidRigType.R6 then
FlyAnimation = Instance.new("Animation")
FlyAnimation.AnimationId = "rbxassetid://" .. FlyAnimR6
else
FlyAnimation = Instance.new("Animation")
FlyAnimation.AnimationId = "rbxassetid://" .. FlyAnimR15
end
FlyTrack = Humanoid:LoadAnimation(FlyAnimation)
FlyTrack.Priority = Enum.AnimationPriority.Action4
FlyTrack:Play()
FlyTrack.Looped = true
end
-- Stop fly animation
local function StopFlyAnimation()
if FlyTrack then
FlyTrack:Stop()
FlyTrack = nil
end
end
-- Fly Init
local function FlyInit()
FlyToggle = true
-- Set up B- "V", "AV"
BV.MaxForce = Vector3.one * math.huge
BAV.MaxTorque = Vector3.one * math.huge
-- Other stuff. Idk
Humanoid.PlatformStand = true
PlayFlyAnimation()
end
-- Init fly if fly toggle already on
if FlyToggle then
FlyInit()
end
-- UnflyUninit
local function UnFlyUninit()
FlyToggle = false
-- Unset up B- "V", "AV"
BV.MaxForce = Vector3.zero
BAV.MaxTorque = Vector3.zero
task.delay(0, function()Humanoid.PlatformStand = false end)
StopFlyAnimation()
end
-- Handle when playuh dies
Humanoid.Died:Connect(function()
UnFlyUninit()
end)
-- Input handler
UserInputService.InputBegan:Connect(function(Input, Gp)
if Gp or TURNOFFSCRIPT then return end
Input = Input.KeyCode.Name:lower()
-- Toggle fly
if Input == FlyKey then
FlyToggle = not FlyToggle
if FlyToggle then
FlyInit()
else
UnFlyUninit()
end
end
-- Update movement keys
if MKeys[Input] ~= nil then
MKeys[Input] = true
end
-- Shut down everything
if Input == "p" then
TURNOFFSCRIPT = true
UnFlyUninit()
end
-- Prompt speed change
if Input == "l" then
ChangeSpeedPrompt()
end
end)
UserInputService.InputEnded:Connect(function(Input, Gp)
if Gp or TURNOFFSCRIPT then return end
Input = Input.KeyCode.Name:lower()
-- Update movement keys
if MKeys[Input] ~= nil then
MKeys[Input] = false
end
end)
-- The FLY handler (Run service :)
RunService.RenderStepped:Connect(function(dt)
if not FlyToggle or TURNOFFSCRIPT then return end
-- Get camera stuff
local CamCF = Camera.CFrame
local F = CamCF.LookVector
local R = CamCF.RightVector
local U = Vector3.new(0, 1, 0)
-- Get move direction (PC)
local MoveDir = Vector3.zero
if MKeys.w then MoveDir += F end
if MKeys.a then MoveDir -= R end
if MKeys.s then MoveDir -= F end
if MKeys.d then MoveDir += R end
if MKeys.q then MoveDir -= U end
if MKeys.e or MKeys.space then MoveDir += U end
-- Get move direction (Mobile)
if MobileMoveVec.Magnitude > 0 then
MoveDir += (F * MobileMoveVec.Y) + (R * MobileMoveVec.X)
end
-- Update movement
if MoveDir.Magnitude > 0 then
MoveDir = MoveDir.Unit * Speed
if UseCFrame then
local NewPos = Root.Position + MoveDir * dt
Root.CFrame = CFrame.new(NewPos) * (Root.CFrame - Root.Position)
else
BV.Velocity = MoveDir
end
else
BV.Velocity = Vector3.zero
end
-- Update rotation:
Root.CFrame = CFrame.new(Root.Position, Root.Position + F)
end)
Comments