Advertisement
OtterX

b

Jan 7th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. local FlyKey = Enum.KeyCode.G
  2. local SpeedKey = Enum.KeyCode.LeftControl
  3.  
  4. local SpeedKeyMultiplier = 2
  5. local FlightSpeed = 200
  6. local FlightAcceleration = 3
  7. local TurnSpeed = 12
  8.  
  9. local UserInputService = game:GetService("UserInputService")
  10. local RunService = game:GetService("RunService")
  11. local Players = game:GetService("Players")
  12. local User = Players.LocalPlayer
  13. local Camera = workspace.CurrentCamera
  14. local UserCharacter = nil
  15. local UserRootPart = nil
  16. local Connection = nil
  17.  
  18. workspace.Changed:Connect(function()
  19. Camera = workspace.CurrentCamera
  20. end)
  21.  
  22. local setCharacter = function(c)
  23. UserCharacter = c
  24. UserRootPart = c:WaitForChild("HumanoidRootPart")
  25. end
  26.  
  27. User.CharacterAdded:Connect(setCharacter)
  28. if User.Character then
  29. setCharacter(User.Character)
  30. end
  31.  
  32. local CurrentVelocity = Vector3.new(0,0,0)
  33. local Flight = function(delta)
  34. local BaseVelocity = Vector3.new(0,0,0)
  35. if not UserInputService:GetFocusedTextBox() then
  36. if UserInputService:IsKeyDown(Enum.KeyCode.W) then
  37. BaseVelocity = BaseVelocity + (Camera.CFrame.LookVector * FlightSpeed)
  38. end
  39. if UserInputService:IsKeyDown(Enum.KeyCode.A) then
  40. BaseVelocity = BaseVelocity - (Camera.CFrame.RightVector * FlightSpeed)
  41. end
  42. if UserInputService:IsKeyDown(Enum.KeyCode.S) then
  43. BaseVelocity = BaseVelocity - (Camera.CFrame.LookVector * FlightSpeed)
  44. end
  45. if UserInputService:IsKeyDown(Enum.KeyCode.D) then
  46. BaseVelocity = BaseVelocity + (Camera.CFrame.RightVector * FlightSpeed)
  47. end
  48. if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
  49. BaseVelocity = BaseVelocity + (Camera.CFrame.UpVector * FlightSpeed)
  50. end
  51. if UserInputService:IsKeyDown(SpeedKey) then
  52. BaseVelocity = BaseVelocity * SpeedKeyMultiplier
  53. end
  54. end
  55. if UserRootPart then
  56. local car = UserRootPart:GetRootPart()
  57. if car.Anchored then return end
  58. if not isnetworkowner(car) then return end
  59. CurrentVelocity = CurrentVelocity:Lerp(
  60. BaseVelocity,
  61. math.clamp(delta * FlightAcceleration, 0, 1)
  62. )
  63. car.Velocity = CurrentVelocity + Vector3.new(0,2,0)
  64. if car ~= UserRootPart then
  65. car.RotVelocity = Vector3.new(0,0,0)
  66. car.CFrame = car.CFrame:Lerp(CFrame.lookAt(
  67. car.Position,
  68. car.Position + CurrentVelocity + Camera.CFrame.LookVector
  69. ), math.clamp(delta * TurnSpeed, 0, 1))
  70. end
  71. end
  72. end
  73.  
  74. UserInputService.InputBegan:Connect(function(userInput,gameProcessed)
  75. if gameProcessed then return end
  76. if userInput.KeyCode == FlyKey then
  77. if Connection then
  78. Connection:Disconnect()
  79. Connection = nil
  80. else
  81. CurrentVelocity = UserRootPart.Velocity
  82. Connection = RunService.Heartbeat:Connect(Flight)
  83. end
  84. end
  85. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement