How would I go about making a unlaggy anti teleport script?

I been thinking of making a making a anti teleport script, I know the basics like detect last position then if gone too far then kick or whatever, but I am asking what would be a unlaggy way of doing it? spamming loops on server wouldn’t too well and I am looking for any other solutions?

The Logic

You only need two loops; no “loop spam” or “laggy” nature should be caused.

  • One to re-initate the inner loop
  • The inner loop to iterate over each player.

The logic: Just store the position of every player and compare it to the previous iteration.

Here is some example code:

This example code does work on some things I did not explain but reading the code you will see I left some nice comments to explain how the code works.


local walkSpeed = 16 --// the walkspeed of a player; could instead be defined in the loop where each player might have different walkspeeds
local maxPing = 2 --// allows each player to have a maximum of 2 second ping (2000ms)

while true do
    for player, lastPosition in pairs(playersIngame) do
        local character = player.Character
        
        if player.Character then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

            if humanoidRootPart then
                local currentPosition = humanoidRootPart.Position
                local distance = (currentPosition - lastPosition).Magnitude
                
                if (distance < (walkSpeed * maxPing)) then
                    playersIngame[player] = currentPosition
                else
                    punish(player) --// punish them in some way
                end
            end
        end
    end

    wait(sleep) --// time between each iteration; good number would be something like 1 becuase at 16 walkspeed a player only moves 16 studs (UNDER 0 PING CONDITIONS; at 2 second ping its around 32)
end
1 Like
  • You could handle these checks per-player whenever they have a character spawned.
  • You can take EpicMetatableMoment’s approach and have two loops that are always active.
  • You could try a decentralized approach and have every player’s client keep track of everyone else. With this approach you would have these clients notify the server when a player teleports. After a certain threshold, the server takes action. This would be more complicated (potentially unnecessary), but it takes the load off of the server and distributes it to the clients.

These are a few ways to go about making an anti-teleport script.

1 Like