Need help with anti tabbing system

When I test the tabbing system in studio it works as intended, but when I test in-game it does not work for some reason

Example:


ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local DetectRemote = Remotes:WaitForChild("DetectRemote")

local maxTime = 3
local cooldown = 1

local currentEventList = {}
local connections = {}

function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local lastUpdated = tick()
	local lastEventCheck

	local function checkForTabbing()
		if character then		
			if tick() - lastUpdated < cooldown then return end
			lastUpdated = tick()
			
			local networkPing = player:GetNetworkPing() * 1000
			print(networkPing)

			DetectRemote:FireClient(player)
			lastEventCheck = tick()
			currentEventList[player] = true

			repeat
				task.wait(1)
				if lastEventCheck and (tick() - lastEventCheck > maxTime) then
					player:Kick("Tabbing detected")
				end
			until not currentEventList[player]

			lastEventCheck = nil
		end
	end

	local function onCharacterAdded(newCharacter)
		character = newCharacter
	end

	player.CharacterAdded:Connect(onCharacterAdded)
	connections[player] = RunService.Heartbeat:Connect(checkForTabbing)
end

function onPlayerRemoving(player)
	if connections[player] then
		connections[player]:Disconnect()
		connections[player] = nil
	end
	currentEventList[player] = nil
end

function onWaitingListSignal(player)
	if currentEventList[player] then
		currentEventList[player] = nil
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
DetectRemote.OnServerEvent:Connect(onWaitingListSignal)

ClientScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local DetectRemote = Remotes:WaitForChild("DetectRemote")

function onSignal()
   DetectRemote:FireServer()
end

DetectRemote.OnClientEvent:Connect(onSignal)
1 Like

-- I don’t really understand what your code is trying to do beyond sending a RemoteEvent and checking how much time it takes until it gets back. (This method will kick players for any lag spikes at all, by the way.)

-- Why are you placing a repeating loop like this inside of a function that will run potentially up to 240 times a second?