Upscalefanatic3

(Roblox) Detect Exploiters In-Game Script

Mar 14th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local players = game:GetService("Players")
  2. local runService = game:GetService("RunService")
  3.  
  4. local detectSpeedHacks = true -- Detect speed hacks by checking the average distance moved.
  5. local detectFlying = true -- Detects fly hacks and HipHeight changes by checking the average height.
  6. local detectGodMode = true -- Detects god mode by checking to see if the player's humanoid was removed (why does that even replicared?!).
  7.  
  8. local stepTime = 0.1 -- Loop check time in seconds.
  9. local maxSteps = 50 -- Max recorded step amount before checks happen (lower = greater false positive chance).
  10. local averageHeightLimit = 5 -- Height Limit for checks in studs. Jumping and falling won't trigger the detection method. Lower = lower HipHeight allowed, five is recommended.
  11. local averageSpeedLimit = 50 -- Speed limit for checks in studs. This will be around the player's WalkSpeed.
  12. local suddenSpeedLimit = 1000 -- Sudden movement amount (effectively teleportation).
  13. local grantMercy = true --If enabled, players will receive strikes instead being labeled as exploiters. This will increase the time by a multiple of StrikeLimit seconds.
  14. local strikeLimit = 3 -- The strike limit if GrantMercy is enabled.
  15.  
  16. -- Previous Time reference for the main loop
  17. local lastTime = 0
  18. local downVector = -Vector3.new(0, math.floor(averageHeightLimit * 1.5), 0) -- 50% more than the down limit (just to make sure that it can detect flying)
  19.  
  20. local enemies = workspace:FindFirstChild("Enemies")
  21.  
  22. local playerReferences = {}
  23.  
  24. local function calculateAverage(tab)
  25.     local total = 0
  26.    
  27.     for i,v in pairs(tab) do
  28.         total = total + v
  29.     end
  30.    
  31.     return total/#tab
  32. end
  33.  
  34. players.PlayerAdded:Connect(function(player)
  35.     playerReferences[player] = {
  36.         PreviousHeights = {},
  37.         PreviousSpeeds = {},
  38.         Strikes = 0, -- This carries over when the player resets.
  39.         LastPosition = nil
  40.     }
  41.    
  42.     player.CharacterAdded:Connect(function(character)
  43.         if detectGodMode then
  44.             character.ChildRemoved:Connect(function(child)
  45.                 if child:IsA("Humanoid") then
  46.                     print(player.Name .. " kicked for god mode")
  47.                     player:Kick("Exploiting - god mode")
  48.                 end
  49.             end)
  50.         end
  51.     end)
  52. end)
  53.  
  54. players.PlayerRemoving:Connect(function(player)
  55.     if playerReferences[player] then
  56.         playerReferences[player] = nil
  57.     end
  58. end)
  59.  
  60. runService.Heartbeat:Connect(function()
  61.     if tick() - lastTime >= stepTime then
  62.         lastTime = tick()
  63.        
  64.         for i,v in pairs(playerReferences) do
  65.             local player = players:FindFirstChild(i.Name)
  66.             if player then
  67.                 local character = player.Character
  68.                 if character then
  69.                     local humanoid = character:FindFirstChildOfClass("Humanoid")
  70.                     local hrp = character:FindFirstChild("HumanoidRootPart")
  71.                     if humanoid and humanoid.Health > 0 and hrp then
  72.                         if humanoid.Health > 0 then
  73.                             local height = 0
  74.                             local state = humanoid:GetState()
  75.                             if state ~= Enum.HumanoidStateType.Jumping and state ~= Enum.HumanoidStateType.Freefall then
  76.                                 local ray = Ray.new(hrp.Position, downVector)
  77.                                 local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {character, enemies})
  78.                                
  79.                                 height = hrp.Position.Y - pos.Y
  80.                             end
  81.                            
  82.                             table.insert(v.PreviousHeights, height)
  83.                             if #v.PreviousHeights > maxSteps then
  84.                                 table.remove(v.PreviousHeights, 1)
  85.                             end
  86.                            
  87.                             if detectFlying then
  88.                                 if #v.PreviousHeights == maxSteps then
  89.                                     local average = calculateAverage(v.PreviousHeights)
  90.                                                                                                    
  91.                                     if average > averageHeightLimit then
  92.                                         if grantMercy then
  93.                                             v.Strikes = v.Strikes + 1
  94.                                             v.PreviousHeights = {}
  95.                                         else
  96.                                             player:Kick(string.format("Exploiting - fly hacking: height = %s, average = %s", height, average))
  97.                                         end
  98.                                     end
  99.                                 end
  100.                             end
  101.                            
  102.                             if detectSpeedHacks then
  103.                                 if v.LastPosition and v.Alive then
  104.                                     local position = Vector2.new(hrp.Position.X, hrp.Position.Z)
  105.                                     local distance = (position - v.LastPosition).Magnitude
  106.                                     local speed = distance/stepTime
  107.                                                                                            
  108.                                     if speed >= suddenSpeedLimit then -- teleportation detection (this could return false postives if exploiters mess with players)
  109.                                         if grantMercy then
  110.                                             v.Strikes = v.Strikes + 1
  111.                                             v.PreviousSpeeds = {}
  112.                                         else
  113.                                             player:Kick("Exploiting - teleporting: speed = " .. speed)
  114.                                         end
  115.                                     else
  116.                                         table.insert(v.PreviousSpeeds, speed)
  117.                                         if #v.PreviousSpeeds > maxSteps then
  118.                                             table.remove(v.PreviousSpeeds, 1)
  119.                                         end
  120.                                        
  121.                                         if #v.PreviousSpeeds == maxSteps then
  122.                                             local average = calculateAverage(v.PreviousSpeeds)
  123.                                
  124.                                             if average >= averageSpeedLimit then
  125.                                                 if grantMercy then
  126.                                                     v.Strikes = v.Strikes + 1
  127.                                                     v.PreviousSpeeds = {}
  128.                                                 else
  129.                                                     player:Kick(string.format("Exploiting - speed hacking: speed = %s, average = %s", speed, average))
  130.                                                 end
  131.                                             end
  132.                                         end
  133.                                     end
  134.                                    
  135.                                     v.LastPosition = position
  136.                                 else
  137.                                     v.LastPosition = Vector2.new(hrp.Position.X, hrp.Position.Z)
  138.                                 end
  139.                             end
  140.                            
  141.                             if v.Strikes >= strikeLimit then
  142.                                 player:Kick("Exploiting")
  143.                             end
  144.                         else
  145.                             v.PreviousHeights = {}
  146.                             v.PreviousSpeeds = {}
  147.                             v.LastPosition = nil
  148.                         end
  149.                     end
  150.                 end
  151.             end
  152.         end
  153.     end
  154. end)
Add Comment
Please, Sign In to add comment