Repository navigation
Expand file tree
/
Copy pathanticheat.lua
More file actions
672 lines (516 loc) · 14.7 KB
/
Copy pathanticheat.lua
File metadata and controls
672 lines (516 loc) · 14.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
--[[
simple server side anti cheat
this is made to detect strange movement without trusting the client.
the system uses multiple checks instead of banning after one weird move.
main checks:
- speed
- teleport distance
- air time
- humanoid states
- impossible vertical movement
- position history
- suspicious velocity
- basic noclip detection
this is meant to be a learning / demonstration project.
]]
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local Config = {
MaxHorizontalSpeed = 45,
MaxVerticalSpeed = 100,
MaxTeleportDistance = 75,
MaxAirTime = 7,
MaxViolation = 10,
CheckInterval = 0.15,
SpawnProtection = 4,
HistorySize = 15,
PositionTolerance = 8,
SuspiciousVelocity = 140,
WarningLimit = 5,
}
local playerData = {}
local function createPlayerData(player)
return {
player = player,
character = nil,
humanoid = nil,
root = nil,
lastPosition = nil,
lastSafePosition = nil,
lastCheck = os.clock(),
joinedAt = os.clock(),
airStart = nil,
lastGrounded = os.clock(),
violations = 0,
samples = 0,
history = {},
flags = {},
lastVelocity = Vector3.zero,
lastCFrame = nil,
frozen = false,
warned = false,
}
end
local function getData(player)
return playerData[player]
end
local function addViolation(data, reason, amount)
amount = amount or 1
data.violations += amount
data.flags[reason] = (data.flags[reason] or 0) + amount
if data.violations > Config.MaxViolation then
data.violations = Config.MaxViolation
end
end
local function removeViolation(data, amount)
amount = amount or 1
data.violations = math.max(0, data.violations - amount)
end
local function isCharacterReady(data)
if not data.character then
return false
end
if not data.humanoid then
return false
end
if not data.root then
return false
end
if data.humanoid.Health <= 0 then
return false
end
return true
end
local function updateCharacter(data)
local character = data.player.Character
if not character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
local root = character:FindFirstChild("HumanoidRootPart")
if not humanoid or not root then
return
end
data.character = character
data.humanoid = humanoid
data.root = root
data.lastPosition = root.Position
data.lastSafePosition = root.Position
data.lastCFrame = root.CFrame
data.airStart = nil
data.lastGrounded = os.clock()
data.lastVelocity = Vector3.zero
end
local function isSpawnProtected(data)
return os.clock() - data.joinedAt < Config.SpawnProtection
end
local function addHistory(data, position)
table.insert(data.history, {
position = position,
time = os.clock(),
})
if #data.history > Config.HistorySize then
table.remove(data.history, 1)
end
end
local function getHorizontalVelocity(velocity)
return Vector3.new(
velocity.X,
0,
velocity.Z
)
end
local function getHorizontalDistance(first, second)
local difference = first - second
return Vector3.new(
difference.X,
0,
difference.Z
).Magnitude
end
local function getDistance(first, second)
return (first - second).Magnitude
end
local function isGrounded(data)
if not data.root then
return false
end
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {data.character}
params.IgnoreWater = true
local origin = data.root.Position
local direction = Vector3.new(0, -5.5, 0)
local result = Workspace:Raycast(
origin,
direction,
params
)
return result ~= nil
end
local function checkHorizontalSpeed(data, delta)
if delta <= 0 then
return
end
local velocity = data.root.AssemblyLinearVelocity
local horizontalVelocity = getHorizontalVelocity(velocity)
local speed = horizontalVelocity.Magnitude
if speed > Config.MaxHorizontalSpeed then
addViolation(data, "high horizontal speed", 1)
else
removeViolation(data, 0.15)
end
end
local function checkVerticalSpeed(data)
local velocity = data.root.AssemblyLinearVelocity
local verticalSpeed = math.abs(velocity.Y)
if verticalSpeed > Config.MaxVerticalSpeed then
addViolation(data, "high vertical speed", 1)
end
end
local function checkVelocity(data)
local velocity = data.root.AssemblyLinearVelocity
local magnitude = velocity.Magnitude
if magnitude > Config.SuspiciousVelocity then
addViolation(data, "suspicious velocity", 2)
end
data.lastVelocity = velocity
end
local function checkTeleport(data)
if not data.lastPosition then
data.lastPosition = data.root.Position
return
end
local currentPosition = data.root.Position
local distance = getDistance(
currentPosition,
data.lastPosition
)
if distance > Config.MaxTeleportDistance then
addViolation(data, "large position change", 3)
return
end
if distance < Config.PositionTolerance then
data.lastSafePosition = currentPosition
end
data.lastPosition = currentPosition
end
local function checkHorizontalTeleport(data)
if not data.lastPosition then
return
end
local currentPosition = data.root.Position
local horizontalDistance = getHorizontalDistance(
currentPosition,
data.lastPosition
)
if horizontalDistance > Config.MaxTeleportDistance then
addViolation(data, "horizontal teleport", 3)
end
end
local function checkAirTime(data, grounded)
if grounded then
data.airStart = nil
data.lastGrounded = os.clock()
return
end
if not data.airStart then
data.airStart = os.clock()
return
end
local airTime = os.clock() - data.airStart
if airTime > Config.MaxAirTime then
addViolation(data, "long air time", 2)
end
end
local function checkVerticalMovement(data)
if #data.history < 3 then
return
end
local current = data.history[#data.history]
local previous = data.history[#data.history - 2]
local timeDifference = current.time - previous.time
if timeDifference <= 0 then
return
end
local heightDifference =
current.position.Y - previous.position.Y
local verticalRate =
math.abs(heightDifference / timeDifference)
if verticalRate > Config.MaxVerticalSpeed * 1.5 then
addViolation(data, "abnormal vertical movement", 1)
end
end
local function checkHumanoidState(data)
local state = data.humanoid:GetState()
if state == Enum.HumanoidStateType.Dead then
return
end
if state == Enum.HumanoidStateType.Seated then
removeViolation(data, 1)
return
end
if state == Enum.HumanoidStateType.Swimming then
removeViolation(data, 0.5)
return
end
if state == Enum.HumanoidStateType.Climbing then
removeViolation(data, 0.25)
return
end
if state == Enum.HumanoidStateType.Physics then
addViolation(data, "physics state", 0.25)
end
end
local function checkNoclip(data)
if not data.root then
return
end
local position = data.root.Position
local direction = Vector3.new(0, 0, 0)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {data.character}
local offsets = {
Vector3.new(0, 0, 0),
Vector3.new(1.5, 0, 0),
Vector3.new(-1.5, 0, 0),
Vector3.new(0, 0, 1.5),
Vector3.new(0, 0, -1.5),
}
for _, offset in ipairs(offsets) do
local origin = position + offset
local result = Workspace:Raycast(
origin,
direction,
params
)
if result then
addViolation(data, "possible noclip", 1)
break
end
end
end
local function checkPositionHistory(data)
if #data.history < 5 then
return
end
local oldest = data.history[1]
local newest = data.history[#data.history]
local distance = getDistance(
newest.position,
oldest.position
)
local timeDifference = newest.time - oldest.time
if timeDifference <= 0 then
return
end
local averageSpeed = distance / timeDifference
if averageSpeed > Config.MaxHorizontalSpeed * 2 then
addViolation(data, "high average movement", 2)
end
end
local function checkCFrame(data)
local currentCFrame = data.root.CFrame
if not data.lastCFrame then
data.lastCFrame = currentCFrame
return
end
local positionDifference =
getDistance(
currentCFrame.Position,
data.lastCFrame.Position
)
if positionDifference > Config.MaxTeleportDistance then
addViolation(data, "cframe position jump", 2)
end
data.lastCFrame = currentCFrame
end
local function checkRootPart(data)
local root = data.root
if not root:IsDescendantOf(Workspace) then
addViolation(data, "invalid root location", 2)
return
end
if not root:IsA("BasePart") then
addViolation(data, "invalid root object", 3)
return
end
end
local function checkHumanoidProperties(data)
local humanoid = data.humanoid
if humanoid.WalkSpeed > Config.MaxHorizontalSpeed then
addViolation(data, "modified walkspeed", 2)
end
if humanoid.JumpPower > 150 then
addViolation(data, "modified jumppower", 2)
end
if humanoid.HipHeight > 20 then
addViolation(data, "modified hipheight", 1)
end
end
local function resetAfterRespawn(data)
data.violations = 0
data.flags = {}
data.history = {}
data.airStart = nil
data.warned = false
if data.root then
data.lastPosition = data.root.Position
data.lastSafePosition = data.root.Position
data.lastCFrame = data.root.CFrame
end
end
local function sendWarning(data)
if data.warned then
return
end
if data.violations < Config.WarningLimit then
return
end
data.warned = true
warn(
"[anti cheat] warning:",
data.player.Name,
"violations:",
data.violations
)
end
local function getTopFlag(data)
local selectedReason = "unknown"
local selectedAmount = 0
for reason, amount in pairs(data.flags) do
if amount > selectedAmount then
selectedAmount = amount
selectedReason = reason
end
end
return selectedReason, selectedAmount
end
local function restorePlayer(data)
if not data.lastSafePosition then
return
end
if not data.root then
return
end
data.root.AssemblyLinearVelocity = Vector3.zero
data.root.AssemblyAngularVelocity = Vector3.zero
data.root.CFrame =
CFrame.new(data.lastSafePosition)
data.lastPosition = data.lastSafePosition
data.lastCFrame = data.root.CFrame
end
local function punishPlayer(data)
if data.violations < Config.MaxViolation then
return
end
local reason = getTopFlag(data)
warn(
"[anti cheat] player detected:",
data.player.Name,
"reason:",
reason
)
restorePlayer(data)
data.violations = math.floor(
Config.MaxViolation / 2
)
data.flags = {}
end
local function runChecks(data, delta)
if not isCharacterReady(data) then
return
end
if isSpawnProtected(data) then
data.lastPosition = data.root.Position
data.lastCFrame = data.root.CFrame
return
end
local grounded = isGrounded(data)
addHistory(
data,
data.root.Position
)
checkHorizontalSpeed(data, delta)
checkVerticalSpeed(data)
checkVelocity(data)
checkTeleport(data)
checkHorizontalTeleport(data)
checkAirTime(data, grounded)
checkVerticalMovement(data)
checkHumanoidState(data)
checkNoclip(data)
checkPositionHistory(data)
checkCFrame(data)
checkRootPart(data)
checkHumanoidProperties(data)
sendWarning(data)
punishPlayer(data)
data.samples += 1
end
local function setupPlayer(player)
local data = createPlayerData(player)
playerData[player] = data
player.CharacterAdded:Connect(function()
task.wait(1)
updateCharacter(data)
resetAfterRespawn(data)
end)
if player.Character then
task.spawn(function()
task.wait(1)
updateCharacter(data)
end)
end
end
local function removePlayer(player)
local data = playerData[player]
if not data then
return
end
data.character = nil
data.humanoid = nil
data.root = nil
data.history = {}
data.flags = {}
playerData[player] = nil
end
for _, player in ipairs(Players:GetPlayers()) do
setupPlayer(player)
end
Players.PlayerAdded:Connect(function(player)
setupPlayer(player)
end)
Players.PlayerRemoving:Connect(function(player)
removePlayer(player)
end)
local accumulator = 0
RunService.Heartbeat:Connect(function(deltaTime)
accumulator += deltaTime
if accumulator < Config.CheckInterval then
return
end
local delta = accumulator
accumulator = 0
for _, data in pairs(playerData) do
if data.player.Parent == Players then
runChecks(data, delta)
end
end
end)
task.spawn(function()
while true do
task.wait(5)
for _, data in pairs(playerData) do
if data.violations > 0 then
removeViolation(data, 1)
end
if data.violations < Config.WarningLimit then
data.warned = false
end
end
end
end)
print("[anti cheat] server movement protection loaded")