· 3 years ago · Nov 29, 2021, 02:20 AM
1if getgenv().Aiming then return getgenv().Aiming end
2
3-- // Services
4local Players = game:GetService("Players")
5local Workspace = game:GetService("Workspace")
6local GuiService = game:GetService("GuiService")
7local RunService = game:GetService("RunService")
8
9-- // Vars
10local Heartbeat = RunService.Heartbeat
11local LocalPlayer = Players.LocalPlayer
12local CurrentCamera = Workspace.CurrentCamera
13local Mouse = LocalPlayer:GetMouse()
14
15-- // Optimisation Vars (ugly)
16local Drawingnew = Drawing.new
17local Color3fromRGB = Color3.fromRGB
18local Vector2new = Vector2.new
19local GetGuiInset = GuiService.GetGuiInset
20local Randomnew = Random.new
21local mathfloor = math.floor
22local CharacterAdded = LocalPlayer.CharacterAdded
23local CharacterAddedWait = CharacterAdded.Wait
24local WorldToViewportPoint = CurrentCamera.WorldToViewportPoint
25local RaycastParamsnew = RaycastParams.new
26local EnumRaycastFilterTypeBlacklist = Enum.RaycastFilterType.Blacklist
27local Raycast = Workspace.Raycast
28local GetPlayers = Players.GetPlayers
29local Instancenew = Instance.new
30local IsDescendantOf = Instancenew("Part").IsDescendantOf
31local FindFirstChildWhichIsA = Instancenew("Part").FindFirstChildWhichIsA
32local FindFirstChild = Instancenew("Part").FindFirstChild
33local tableremove = table.remove
34local tableinsert = table.insert
35
36-- // Silent Aim Vars
37getgenv().Aiming = {
38 Enabled = true,
39
40 ShowFOV = true,
41 FOV = 60,
42 FOVSides = 12,
43 FOVColour = Color3fromRGB(231, 84, 128),
44
45 VisibleCheck = true,
46
47 HitChance = 100,
48
49 Selected = nil,
50 SelectedPart = nil,
51
52 TargetPart = {"Head", "HumanoidRootPart"},
53
54 Ignored = {
55 Teams = {
56 {
57 Team = LocalPlayer.Team,
58 TeamColor = LocalPlayer.TeamColor,
59 },
60 },
61 Players = {
62 LocalPlayer,
63 91318356
64 }
65 },
66
67 RaycastIgnore = nil
68}
69local Aiming = getgenv().Aiming
70
71-- // Create circle
72local circle = Drawingnew("Circle")
73circle.Transparency = 1
74circle.Thickness = 2
75circle.Color = Aiming.FOVColour
76circle.Filled = false
77Aiming.FOVCircle = circle
78
79-- // Update
80function Aiming.UpdateFOV()
81 -- // Make sure the circle exists
82 if not (circle) then
83 return
84 end
85
86 -- // Set Circle Properties
87 circle.Visible = Aiming.ShowFOV
88 circle.Radius = (Aiming.FOV * 3)
89 circle.Position = Vector2new(Mouse.X, Mouse.Y + GetGuiInset(GuiService).Y)
90 circle.NumSides = Aiming.FOVSides
91 circle.Color = Aiming.FOVColour
92
93 -- // Return circle
94 return circle
95end
96
97-- // Custom Functions
98local CalcChance = function(percentage)
99 -- // Floor the percentage
100 percentage = mathfloor(percentage)
101
102 -- // Get the chance
103 local chance = mathfloor(Randomnew().NextNumber(Randomnew(), 0, 1) * 100) / 100
104
105 -- // Return
106 return chance <= percentage / 100
107end
108
109-- // Customisable Checking Functions: Is a part visible
110function Aiming.IsPartVisible(Part, PartDescendant)
111 -- // Vars
112 local Character = LocalPlayer.Character or CharacterAddedWait(CharacterAdded)
113 local Origin = CurrentCamera.CFrame.Position
114 local _, OnScreen = WorldToViewportPoint(CurrentCamera, Part.Position)
115
116 -- //
117 if (OnScreen) then
118 -- // Vars
119 local raycastParams = RaycastParamsnew()
120 raycastParams.FilterType = EnumRaycastFilterTypeBlacklist
121 raycastParams.FilterDescendantsInstances = Aiming.RaycastIgnore or {Character, CurrentCamera}
122
123 -- // Cast ray
124 local Result = Raycast(Workspace, Origin, Part.Position - Origin, raycastParams)
125
126 -- // Make sure we get a result
127 if (Result) then
128 -- // Vars
129 local PartHit = Result.Instance
130 local Visible = (not PartHit or IsDescendantOf(PartHit, PartDescendant))
131
132 -- // Return
133 return Visible
134 end
135 end
136
137 -- // Return
138 return false
139end
140
141-- // Ignore player
142function Aiming.IgnorePlayer(Player)
143 -- // Vars
144 local Ignored = Aiming.Ignored
145 local IgnoredPlayers = Ignored.Players
146
147 -- // Find player in table
148 for _, IgnoredPlayer in ipairs(IgnoredPlayers) do
149 -- // Make sure player matches
150 if (IgnoredPlayer == Player) then
151 return false
152 end
153 end
154
155 -- // Blacklist player
156 tableinsert(IgnoredPlayers, Player)
157 return true
158end
159
160-- // Unignore Player
161function Aiming.UnIgnorePlayer(Player)
162 -- // Vars
163 local Ignored = Aiming.Ignored
164 local IgnoredPlayers = Ignored.Players
165
166 -- // Find player in table
167 for i, IgnoredPlayer in ipairs(IgnoredPlayers) do
168 -- // Make sure player matches
169 if (IgnoredPlayer == Player) then
170 -- // Remove from ignored
171 tableremove(IgnoredPlayers, i)
172 return true
173 end
174 end
175
176 -- //
177 return false
178end
179
180-- // Ignore team
181function Aiming.IgnoreTeam(Team, TeamColor)
182 -- // Vars
183 local Ignored = Aiming.Ignored
184 local IgnoredTeams = Ignored.Teams
185
186 -- // Find team in table
187 for _, IgnoredTeam in ipairs(IgnoredTeams) do
188 -- // Make sure team matches
189 if (IgnoredTeam.Team == Team and IgnoredTeam.TeamColor == TeamColor) then
190 return false
191 end
192 end
193
194 -- // Ignore team
195 tableinsert(IgnoredTeams, {Team, TeamColor})
196 return true
197end
198
199-- // Unignore team
200function Aiming.UnIgnoreTeam(Team, TeamColor)
201 -- // Vars
202 local Ignored = Aiming.Ignored
203 local IgnoredTeams = Ignored.Teams
204
205 -- // Find team in table
206 for i, IgnoredTeam in ipairs(IgnoredTeams) do
207 -- // Make sure team matches
208 if (IgnoredTeam.Team == Team and IgnoredTeam.TeamColor == TeamColor) then
209 -- // Remove
210 tableremove(IgnoredTeams, i)
211 return true
212 end
213 end
214
215 -- // Return
216 return false
217end
218
219-- // Toggle team check
220function Aiming.TeamCheck(Toggle)
221 if (Toggle) then
222 return Aiming.IgnoreTeam(LocalPlayer.Team, LocalPlayer.TeamColor)
223 end
224
225 return Aiming.UnIgnoreTeam(LocalPlayer.Team, LocalPlayer.TeamColor)
226end
227
228-- // Check teams
229function Aiming.IsIgnoredTeam(Player)
230 -- // Vars
231 local Ignored = Aiming.Ignored
232 local IgnoredTeams = Ignored.Teams
233
234 -- // Check if team is ignored
235 for _, IgnoredTeam in ipairs(IgnoredTeams) do
236 -- // Make sure team matches
237 if (Player.Team == IgnoredTeam.Team and Player.TeamColor == IgnoredTeam.TeamColor) then
238 return true
239 end
240 end
241
242 -- // Return
243 return false
244end
245
246-- // Check if player (and team) is ignored
247function Aiming.IsIgnored(Player)
248 -- // Vars
249 local Ignored = Aiming.Ignored
250 local IgnoredPlayers = Ignored.Players
251
252 -- // Loop
253 for _, IgnoredPlayer in ipairs(IgnoredPlayers) do
254 -- // Check if Player Id
255 if (typeof(IgnoredPlayer) == "number" and Player.UserId == IgnoredPlayer) then
256 return true
257 end
258
259 -- // Normal Player Instance
260 if (IgnoredPlayer == Player) then
261 return true
262 end
263 end
264
265 -- // Team check
266 return Aiming.IsIgnoredTeam(Player)
267end
268
269-- // Get the Direction, Normal and Material
270function Aiming.Raycast(Origin, Destination, UnitMultiplier)
271 if (typeof(Origin) == "Vector3" and typeof(Destination) == "Vector3") then
272 -- // Handling
273 if (not UnitMultiplier) then UnitMultiplier = 1 end
274
275 -- // Vars
276 local Direction = (Destination - Origin).Unit * UnitMultiplier
277 local Result = Raycast(Workspace, Origin, Direction)
278
279 -- // Make sure we have a result
280 if (Result) then
281 local Normal = Result.Normal
282 local Material = Result.Material
283
284 return Direction, Normal, Material
285 end
286 end
287
288 -- // Return
289 return nil
290end
291
292-- // Get Character
293function Aiming.Character(Player)
294 return Player.Character
295end
296
297-- // Check Health
298function Aiming.CheckHealth(Player)
299 -- // Get Humanoid
300 local Character = Aiming.Character(Player)
301 local Humanoid = FindFirstChildWhichIsA(Character, "Humanoid")
302
303 -- // Get Health
304 local Health = (Humanoid and Humanoid.Health or 0)
305
306 -- //
307 return Health > 0
308end
309
310-- // Check if silent aim can used
311function Aiming.Check()
312 return (Aiming.Enabled == true and Aiming.Selected ~= LocalPlayer and Aiming.SelectedPart ~= nil)
313end
314Aiming.checkSilentAim = Aiming.Check
315
316-- // Get Closest Target Part
317function Aiming.GetClosestTargetPartToCursor(Character)
318 local TargetParts = Aiming.TargetPart
319
320 -- // Vars
321 local ClosestPart = nil
322 local ClosestPartPosition = nil
323 local ClosestPartOnScreen = false
324 local ClosestPartMagnitudeFromMouse = nil
325 local ShortestDistance = 1/0
326
327 -- //
328 local function CheckTargetPart(TargetPart)
329 -- // Convert string -> Instance
330 if (typeof(TargetPart) == "string") then
331 TargetPart = FindFirstChild(Character, TargetPart)
332 end
333
334 -- // Make sure we have a target
335 if not (TargetPart) then
336 return
337 end
338
339 -- // Get the length between Mouse and Target Part (on screen)
340 local PartPos, onScreen = WorldToViewportPoint(CurrentCamera, TargetPart.Position)
341 local GuiInset = GetGuiInset(GuiService)
342 local Magnitude = (Vector2new(PartPos.X, PartPos.Y - GuiInset.Y) - Vector2new(Mouse.X, Mouse.Y)).Magnitude
343
344 -- //
345 if (Magnitude < ShortestDistance) then
346 ClosestPart = TargetPart
347 ClosestPartPosition = PartPos
348 ClosestPartOnScreen = onScreen
349 ClosestPartMagnitudeFromMouse = Magnitude
350 ShortestDistance = Magnitude
351 end
352 end
353
354 -- // String check
355 if (typeof(TargetParts) == "string") then
356 -- // Check if it all
357 if (TargetParts == "All") then
358 -- // Loop through character children
359 for _, v in ipairs(Character:GetChildren()) do
360 -- // See if it a part
361 if not (v:IsA("BasePart")) then
362 continue
363 end
364
365 -- // Check it
366 CheckTargetPart(v)
367 end
368 else
369 -- // Individual
370 CheckTargetPart(TargetParts)
371 end
372 end
373
374 -- //
375 if (typeof(TargetParts) == "table") then
376 -- // Loop through all target parts and check them
377 for _, TargetPartName in ipairs(TargetParts) do
378 CheckTargetPart(TargetPartName)
379 end
380 end
381
382 -- //
383 return ClosestPart, ClosestPartPosition, ClosestPartOnScreen, ClosestPartMagnitudeFromMouse
384end
385
386-- // Silent Aim Function
387function Aiming.GetClosestPlayerToCursor()
388 -- // Vars
389 local TargetPart = nil
390 local ClosestPlayer = nil
391 local Chance = CalcChance(Aiming.HitChance)
392 local ShortestDistance = 1/0
393
394 -- // Chance
395 if (not Chance) then
396 Aiming.Selected = LocalPlayer
397 Aiming.SelectedPart = nil
398
399 return LocalPlayer
400 end
401
402 -- // Loop through all players
403 for _, Player in ipairs(GetPlayers(Players)) do
404 -- // Get Character
405 local Character = Aiming.Character(Player)
406
407 -- // Make sure isn't ignored and Character exists
408 if (Aiming.IsIgnored(Player) == false and Character) then
409 -- // Vars
410 local TargetPartTemp, _, _, Magnitude = Aiming.GetClosestTargetPartToCursor(Character)
411
412 -- // Check if part exists and health
413 if (TargetPartTemp and Aiming.CheckHealth(Player)) then
414 -- // Check if is in FOV
415 if (circle.Radius > Magnitude and Magnitude < ShortestDistance) then
416 -- // Check if Visible
417 if (Aiming.VisibleCheck and not Aiming.IsPartVisible(TargetPartTemp, Character)) then continue end
418
419 -- // Set vars
420 ClosestPlayer = Player
421 ShortestDistance = Magnitude
422 TargetPart = TargetPartTemp
423 end
424 end
425 end
426 end
427
428 -- // End
429 Aiming.Selected = ClosestPlayer
430 Aiming.SelectedPart = TargetPart
431end
432
433-- // Heartbeat Function
434Heartbeat:Connect(function()
435 Aiming.UpdateFOV()
436 Aiming.GetClosestPlayerToCursor()
437end)
438
439-- //
440return Aiming
441
442-- // If you want the examples, look at the docs.