My attempt at an anti speed script

Hi, I made an anti speed script the other day and noticed it was doing well, so I decided to come here for further review and make sure I didn’t miss anything. Let me know what you think and what I could improve, thanks!

Edit: The script should be a server script in ServerScriptService

--------------------------------------------------------------------------------------------------
local ChecksPerLoop = 3 --Points of reference to calculate the total difference traveled.
local TimeToComplete = 2 --Total time it take for each loop to complete. In seconds.
local MaxOffenses = 7 --Offenses before the user is kicked from the server.
local InstantKick = 2000 --Distance in studs the user must travel in order to be instantly kicked.
local ThreshHoldSensitivity = 2 --Lower = more sensitive. Default is 2.

local DebugStats = false --Turn to true if you want analytics printed in console.
--------------------------------------------------------------------------------------------------

game.Players.PlayerAdded:Connect(function(p)
	local sessionOffenses = {}
	p.CharacterAdded:Connect(function(c)
		local root = c.HumanoidRootPart
		local hum = c.Humanoid

		while root do
			local verifiedTeleport = false

			local newPosition = root.Position
			local lastPosition = root.Position
			
			local beforeSpeed = hum.WalkSpeed
				
			local distTravelled = 0
				
			local adjustedSpeed = 0

			pcall(function()
				hum.Died:Connect(function()
					verifiedTeleport = true
				end)
			end)
			--Checks if the player dies. If the user dies and teleports to spawn then the loop current check will not run.
			
			for i=1,ChecksPerLoop,1 do
				wait(TimeToComplete/ChecksPerLoop)
				if c then
					adjustedSpeed = adjustedSpeed + (hum.WalkSpeed/ChecksPerLoop)
					newPosition = root.Position
					distTravelled = distTravelled + (newPosition*Vector3.new(1,0,1) - lastPosition*Vector3.new(1,0,1)).magnitude
					lastPosition = root.Position
				end
			end
			
			if math.round(beforeSpeed) ~= math.round(adjustedSpeed) then
				adjustedSpeed = beforeSpeed+adjustedSpeed
			end

			local thresh = (adjustedSpeed*TimeToComplete)*ThreshHoldSensitivity
			
			if DebugStats then 
				local displayDist, displayThresh, displayDiff = math.round(distTravelled*10)/10, math.round(thresh*10)/10, math.round((distTravelled-thresh)*10)/10
				warn("Distance Traveled: "..displayDist.." Threshold: "..displayThresh.." Distane Under Threshold: "..displayDiff)
			end
			
			if distTravelled > thresh and not verifiedTeleport then
				table.insert(sessionOffenses, math.floor((distTravelled - thresh)))
				--Inserts how much the player has gone over the threshhold
				root.CFrame = CFrame.new(lastPosition)
				--Teleports the player to the last position if over the threshold.
				warn(p.Name.." may be speed hacking. "..math.floor((distTravelled - (4 * adjustedSpeed))))
				--Warns in the developer console once the user travels over the threshold
				if #sessionOffenses >= MaxOffenses or (distTravelled - (TimeToComplete * adjustedSpeed)) >= InstantKick then
					p:Kick("The server has encountered an issue, please rejoin.")
					--Do something once the the user's session offenses has gone over the maximum offenses.
				end	
			end
		end
	end)
end)
9 Likes