Advertisement
NebulaZorua

fly script

Aug 10th, 2018
2,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. -- The following code should be in a local script.
  2. -- Only works on PC, not xbox or mobile. I do not have devices to test on.
  3. -- Call the start fly function AFTER the character exists to fly. The function does not run if there is no character.
  4.  
  5. local lastSpace = 0;
  6. local speed = 50 -- This is the fly speed. Change it to whatever you like. The variable can be changed while running
  7.  
  8. local c
  9. local h
  10. local bv
  11. local bav
  12. local cam
  13. local flying
  14. local p = game.Players.LocalPlayer
  15. local buttons = {W = false, S = false, A = false, D = false, Moving = false}
  16.  
  17. local startFly = function () -- Call this function to begin flying
  18. if not p.Character or not p.Character.Head or flying then return end
  19. c = p.Character
  20. h = c.Humanoid
  21. h.PlatformStand = true
  22. cam = workspace:WaitForChild('Camera')
  23. bv = Instance.new("BodyVelocity")
  24. bav = Instance.new("BodyAngularVelocity")
  25. bv.Velocity, bv.MaxForce, bv.P = Vector3.new(0, 0, 0), Vector3.new(10000, 10000, 10000), 1000
  26. bav.AngularVelocity, bav.MaxTorque, bav.P = Vector3.new(0, 0, 0), Vector3.new(10000, 10000, 10000), 1000
  27. bv.Parent = c.Head
  28. bav.Parent = c.Head
  29. flying = true
  30. h.Died:connect(function() flying = false end)
  31. end
  32.  
  33. local endFly = function () -- Call this function to stop flying
  34. if not p.Character or not flying then return end
  35. h.PlatformStand = false
  36. bv:Destroy()
  37. bav:Destroy()
  38. flying = false
  39. end
  40.  
  41. game:GetService("UserInputService").InputBegan:connect(function (input, GPE)
  42. if GPE then return end
  43. for i, e in pairs(buttons) do
  44. if i ~= "Moving" and input.KeyCode == Enum.KeyCode[i] then
  45. buttons[i] = true
  46. buttons.Moving = true
  47. end
  48. end
  49. print(time()-lastSpace)
  50. if(input.KeyCode == Enum.KeyCode.Space)then
  51. if(time()-lastSpace <= .25)then
  52. if(not flying)then startFly() else endFly() end
  53. end
  54. lastSpace = time()
  55. end
  56. end)
  57.  
  58. game:GetService("UserInputService").InputEnded:connect(function (input, GPE)
  59. if GPE then return end
  60. local a = false
  61. for i, e in pairs(buttons) do
  62. if i ~= "Moving" then
  63. if input.KeyCode == Enum.KeyCode[i] then
  64. buttons[i] = false
  65. end
  66. if buttons[i] then a = true end
  67. end
  68. end
  69. buttons.Moving = a
  70. end)
  71.  
  72. local setVec = function (vec)
  73. return vec * (speed / vec.Magnitude)
  74. end
  75.  
  76. game:GetService("RunService").Heartbeat:connect(function (step) -- The actual fly function, called every frame
  77. if flying and c and c.PrimaryPart then
  78. local p = c.PrimaryPart.Position
  79. local cf = cam.CFrame
  80. local ax, ay, az = cf:toEulerAnglesXYZ()
  81. c:SetPrimaryPartCFrame(CFrame.new(p.x, p.y, p.z) * CFrame.Angles(ax, ay, az))
  82. if buttons.Moving then
  83. local t = Vector3.new()
  84. if buttons.W then t = t + (setVec(cf.lookVector)) end
  85. if buttons.S then t = t - (setVec(cf.lookVector)) end
  86. if buttons.A then t = t - (setVec(cf.rightVector)) end
  87. if buttons.D then t = t + (setVec(cf.rightVector)) end
  88. c:TranslateBy(t * step)
  89. end
  90. end
  91. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement