-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFly.lua
More file actions
146 lines (129 loc) · 4.84 KB
/
Fly.lua
File metadata and controls
146 lines (129 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- ===== Universal Fly Script (R6/R15 Safe) =====
-- Toggle: F
-- Move: W A S D
-- Ascend: Space | Descend: LeftControl
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
local hrp = char:FindFirstChild("HumanoidRootPart") or char:FindFirstChild("Torso") or char:FindFirstChild("UpperTorso")
-- Settings
local TOGGLE_KEY = Enum.KeyCode.F
local Speed = 80
local VerticalSpeed = 60
local MaxForce = 1e5
local AutoAnchor = false
-- Internal state
local Active = false
local bv, bg
local conn
local input = { fwd=false, back=false, left=false, right=false, up=false, down=false }
local function ensureParts()
if not char or not char.Parent then
char = player.Character or player.CharacterAdded:Wait()
end
humanoid = char:FindFirstChildOfClass("Humanoid")
hrp = char:FindFirstChild("HumanoidRootPart") or char:FindFirstChild("Torso") or char:FindFirstChild("UpperTorso")
return hrp ~= nil
end
local function destroyControllers()
if bv and bv.Parent then pcall(function() bv:Destroy() end) end
if bg and bg.Parent then pcall(function() bg:Destroy() end) end
if hrp and AutoAnchor then
pcall(function() hrp.Anchored = false end)
end
bv = nil; bg = nil
end
local function createControllers()
if not ensureParts() then return end
destroyControllers()
if AutoAnchor then
pcall(function() hrp.Anchored = true end)
return
end
bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(MaxForce, MaxForce, MaxForce)
bv.P = 2000
bv.Parent = hrp
bg = Instance.new("BodyGyro")
bg.MaxTorque = Vector3.new(MaxForce, MaxForce, MaxForce)
bg.P = 3000
bg.Parent = hrp
end
local function getMoveVector()
local mv = Vector3.new(0,0,0)
if not Camera then return mv end
local look = Camera.CFrame.LookVector
local right = Camera.CFrame.RightVector
if input.fwd then mv = mv + Vector3.new(look.X, 0, look.Z) end
if input.back then mv = mv - Vector3.new(look.X, 0, look.Z) end
if input.right then mv = mv + Vector3.new(right.X, 0, right.Z) end
if input.left then mv = mv - Vector3.new(right.X, 0, right.Z) end
if mv.Magnitude > 0 then mv = mv.Unit end
return mv
end
local function startHeartbeat()
if conn then conn:Disconnect() end
conn = RunService.Heartbeat:Connect(function()
if not Active then return end
if not ensureParts() then return end
if bg and bv then
bg.CFrame = CFrame.new(hrp.Position, hrp.Position + Vector3.new(Camera.CFrame.LookVector.X, 0, Camera.CFrame.LookVector.Z))
local mv = getMoveVector()
local horiz = mv * Speed
local vy = 0
if input.up then vy = VerticalSpeed end
if input.down then vy = -VerticalSpeed end
bv.Velocity = Vector3.new(horiz.X, vy, horiz.Z)
end
end)
end
local function stopHeartbeat()
if conn then conn:Disconnect() conn = nil end
end
local function enableFly()
if Active then return end
if not ensureParts() then return end
Active = true
if humanoid then pcall(function() humanoid.PlatformStand = true end) end
createControllers()
startHeartbeat()
end
local function disableFly()
if not Active then return end
Active = false
stopHeartbeat()
destroyControllers()
if humanoid then pcall(function() humanoid.PlatformStand = false end) end
end
-- Input handlers
UserInputService.InputBegan:Connect(function(io, gp)
if gp then return end
local kc = io.KeyCode
if kc == TOGGLE_KEY then
if Active then disableFly() else enableFly() end
end
if kc == Enum.KeyCode.W then input.fwd = true end
if kc == Enum.KeyCode.S then input.back = true end
if kc == Enum.KeyCode.A then input.left = true end
if kc == Enum.KeyCode.D then input.right = true end
if kc == Enum.KeyCode.Space then input.up = true end
if kc == Enum.KeyCode.LeftControl then input.down = true end
end)
UserInputService.InputEnded:Connect(function(io, gp)
if gp then return end
local kc = io.KeyCode
if kc == Enum.KeyCode.W then input.fwd = false end
if kc == Enum.KeyCode.S then input.back = false end
if kc == Enum.KeyCode.A then input.left = false end
if kc == Enum.KeyCode.D then input.right = false end
if kc == Enum.KeyCode.Space then input.up = false end
if kc == Enum.KeyCode.LeftControl then input.down = false end
end)
-- Cleanup
player.CharacterRemoving:Connect(function() disableFly() end)
player.CharacterAdded:Connect(function(c) char = c ensureParts() end)
print("[Universal Fly] Loaded. Press '"..tostring(TOGGLE_KEY.Name).."' to toggle fly.")