Anti cheat teleport

I have a question. So I need to script a anti-teleport script and I already scripted, but I’m not sure how “disable” it, when you are teleported back to the spawn:

local lastPosition = {}

local function newPlayer(plr)
	plr.CharacterAdded:Connect(function(char)
		lastPosition[plr.UserId] = nil
	end)
	
	spawn(function()
		while true do
			wait(1)
			local char = plr.Character
			if char ~= nil then
				local humanoid = char:FindFirstChild("Humanoid")
				if humanoid ~= nil and char.PrimaryPart ~= nil then
					local currentPosition = char:GetPrimaryPartCFrame()
					local tempLastPosition = lastPosition[plr.UserId]
					if tempLastPosition ~= nil then
						local beforePosition = Vector3.new(tempLastPosition.X, 0, tempLastPosition.Z)
						local newPosition = Vector3.new(currentPosition.X, 0, currentPosition.Z)
						
						local distance = (beforePosition - newPosition).Magnitude
						local maxDistance = humanoid.WalkSpeed*2
						if distance > maxDistance then
							char:SetPrimaryPartCFrame(tempLastPosition)
							continue
						end
					end
					lastPosition[plr.UserId] = currentPosition
				end
			end
		end
	end)
end

players.PlayerAdded:Connect(newPlayer)
players.PlayerRemoving:Connect(remove)
for _, player in pairs(players:GetPlayers()) do
	newPlayer(player)
end```
1 Like

make a value that indicates if player is teleported by server or client, to do this make when player dies, teleports or something, it add value to the server storage, and if it’s value is true, disable anticheat for player, but if it’s false, then use anticheat. Btw, don’t use spawn function() it’s laggy and old, you could use corountines or simple while loop with for loop, for loop every second is better than 30 threads!