This code serves as an **anti-cheat handler** for a Roblox...

September 4, 2025 at 04:33 PM

--// AntiCheat Handler -- Put this in ServerScriptService local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local BanStore = DataStoreService:GetDataStore("TempBanSystem") --// CONFIG local SAFE_CENTER = Vector3.new(0, 0, 0) -- map center (set to your spawn area) local SAFE_RADIUS = 200 -- how far players can normally go local OUT_OF_BOUNDS = 15 -- extra studs allowed past SAFE_RADIUS local BAN_DURATION = 12 * 60 * 60 -- 12 hours in seconds local MAX_DISTANCE_PER_MIN = 70 -- studs per 60s allowed local MAX_TELEPORT = 150 -- max distance instantly (still needed) --// PERMISSIONS local OwnerIds = {1234567890} -- Replace with your Roblox UserId(s) local ModIds = {9876543210} -- Replace with your mod UserId(s) --// Tracking local Violations = {} local LastPositions = {} local MovementHistory = {} --// Utils local function isPrivileged(userId) for _, id in ipairs(OwnerIds) do if userId == id then return true end end for _, id in ipairs(ModIds) do if userId == id then return true end end return false end local function log(player, message) warn("[AntiCheat] " .. player.Name .. " | " .. message) end local function isBanned(userId) local banData = BanStore:GetAsync("Ban_" .. userId) if banData then if os.time() < banData then return true else BanStore:RemoveAsync("Ban_" .. userId) end end return false end local function banPlayer(player) local expireTime = os.time() + BAN_DURATION BanStore:SetAsync("Ban_" .. player.UserId, expireTime) log(player, "BANNED for 12 hours.") player:Kick("You are banned for 12 hours (AntiCheat).") end --// Main monitoring local function monitorPlayer(player) if isPrivileged(player.UserId) then log(player, "is privileged (Owner/Mod) -> Skipping AntiCheat") return -- Skip all checks end Violations[player.UserId] = 0 LastPositions[player.UserId] = nil MovementHistory[player.UserId] = {} player.CharacterAdded:Connect(function(char) local hrp = char:WaitForChild("HumanoidRootPart") local humanoid = char:WaitForChild("Humanoid") task.spawn(function() while char.Parent do task.wait(1) -- check every second local pos = hrp.Position local lastPos = LastPositions[player.UserId] LastPositions[player.UserId] = pos -- Out of bounds check local distanceFromCenter = (pos - SAFE_CENTER).Magnitude if distanceFromCenter > SAFE_RADIUS + OUT_OF_BOUNDS then humanoid.Health = 0 Violations[player.UserId] += 1 log(player, "Went out of bounds (" .. Violations[player.UserId] .. " times)") if Violations[player.UserId] >= 8 then banPlayer(player) break elseif Violations[player.UserId] >= 4 then log(player, "KICKED for leaving bounds too often") player:Kick("You were kicked for repeatedly leaving the map bounds.") break end end -- Movement tracking if lastPos then local distanceMoved = (pos - lastPos).Magnitude -- Teleport check if distanceMoved > MAX_TELEPORT then log(player, "Possible teleport detected (" .. math.floor(distanceMoved) .. " studs)") player:Kick("Kicked by AntiCheat: Illegal teleport detected.") break end -- Store distance for rolling 60s window local history = MovementHistory[player.UserId] table.insert(history, {time = os.time(), dist = distanceMoved}) -- Remove entries older than 60s local cutoff = os.time() - 60 while #history > 0 and history[1].time < cutoff do table.remove(history, 1) end -- Sum total distance in last 60s local total = 0 for _, entry in ipairs(history) do total += entry.dist end -- Kick if too far if total > MAX_DISTANCE_PER_MIN then log(player, "KICKED for moving " .. math.floor(total) .. " studs in 60s (limit " .. MAX_DISTANCE_PER_MIN .. ")") player:Kick("Kicked by AntiCheat: Moving too far too quickly.") break end end end end) end) end --// Player join Players.PlayerAdded:Connect(function(player) if isPrivileged(player.UserId) then log(player, "is privileged (Owner/Mod) -> Skipping AntiCheat") return end if isBanned(player.UserId) then player:Kick("You are temporarily banned. T

This code serves as an anti-cheat handler for a Roblox game. The purpose of the script is to monitor and regulate player behavior, identifying and addressing suspicious activities such as teleportation, excessive movement speed, or leaving the designated map area. It also manages temporary bans for rule violations. Here's a detailed breakdown of what the code does:


Key Functionalities:

  1. Privilege Checking for Owners/Moderators:

    • The OwnerIds and ModIds tables list user IDs that are exempt from Anti-Cheat restrictions (e.g., game creators or trusted moderators).
    • The isPrivileged function checks whether a player's user ID is in these tables and skips all anti-cheat monitoring for those users.
  2. Temporary Ban System:

    • Players who violate certain rules (e.g., leaving the map repeatedly or cheating) get temporarily banned for a configurable duration (BAN_DURATION, default is 12 hours).
    • This is managed using Roblox's DataStoreService to save and persist bans across game sessions.
    • The isBanned function checks if a player is temporarily banned upon joining, and if so, kicks them from the game with a message.
  3. Out of Bounds Detection:

    • The game considers the area defined by SAFE_CENTER and SAFE_RADIUS as the safe play zone.
    • If a player moves significantly beyond this area (above SAFE_RADIUS + OUT_OF_BOUNDS), their humanoid's health is reduced to 0, effectively killing them in-game.
    • Repeated violations (more than 4) result in kicks, and 8 violations lead to a temporary ban.
  4. Teleportation Detection:

    • The script tracks player positions (LastPositions) and checks if the distance between two consecutive positions exceeds MAX_TELEPORT (the maximum allowed movement in one second).
    • Players who teleport (exceeding MAX_TELEPORT) are immediately kicked from the game.
  5. Excessive Speed Detection:

    • Tracks the player's movement over a rolling 60-second window using MovementHistory.
    • Using this history, the script sums up the total distance moved in the last 60 seconds. If the total exceeds MAX_DISTANCE_PER_MIN (default is 70 studs), the player is kicked for "moving too far too quickly."
  6. Player Monitoring:

    • Each player's position, movement speed, and violations are monitored in real-time upon joining and spawning their character.
    • Violations (e.g., leaving the map or teleporting) are counted (Violations table), and actions (e.g., kicking or banning) are escalated depending on the number of infractions.
  7. Logging:

    • The log function generates warnings in the server output for all significant actions, such as bans, kicks, or detected rule violations.
  8. Automatic Cleanup:

    • Older movement records (MovementHistory) are cleaned up regularly to only store data from the last 60 seconds.
    • Expired bans are automatically removed when players try to join after their ban duration has elapsed.

Use Case:

The script is ideal for detecting and addressing common cheating methods, such as:

  • Speed hacks: Moving faster than allowed (MAX_DISTANCE_PER_MIN).
  • Teleport hacks: Instantaneously moving a large distance between ticks (MAX_TELEPORT).
  • Leaving the map: Exceeding the predefined boundary (SAFE_RADIUS + OUT_OF_BOUNDS).

Flow of Events:

  1. Player Joins:

    • The script checks if the player is banned. If banned, they are kicked. Otherwise, the player is monitored.
  2. Character Added:

    • A new thread starts to monitor the player's HumanoidRootPart position every second.
    • Violations are tracked and handled (kicking or banning as needed).
  3. Violation Actions:

    • Going Out of Bounds: Kills the player and tracks the number of violations; kicks or bans after repeated offenses.
    • Teleporting: Kicks the player immediately if illegal teleportation is detected.
    • Excessive Movement: Kicks the player for moving too far within a 60-second window.

Final Notes:

This script is designed to be placed in the ServerScriptService of a Roblox game to secure the server from cheating or exploiting behaviors. It uses server-side checks to ensure that client-based hacks have minimal impact, and any suspicious activity results in immediate disconnection or banning from the game.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node