What to do if wait() makes your game feel laggy

I have a game where I am making an admin panel that anyone can use, however the more destructive commands like kill all and BTools have an anticheat system around them. for players who want to use these, they have to be whitelisted, the problem is that anticheat system takes a little bit to go through and makes the game look laggy.

the way my GUI works is the following:

first, a local script sends this:

Auth:FireServer("stop reading my scripts", "Cheese")

then, my anticheat script does this:

math.randomseed(tick())

local Code
local Auth = game.ReplicatedStorage.AuthRequest
local BEvent = game.ReplicatedStorage.AuthEvent
local Package = game.ReplicatedStorage.SendPackage
local Count = 0

Auth.OnServerEvent:Connect(function(Plr, Auth1, Auth2)
	
	local function CheckPlr()
		for i, v in ipairs(game.Players:GetChildren()) do
			if v.Name == Plr.Name then
				return true
			elseif Count == #game.Players:GetChildren() then
				return false
		end
		Count = Count + 1
		end
	end
	
	if CheckPlr() == true and Auth1 == "stop reading my scripts" and Auth2 == "Cheese" then
		Code = math.random(1, 1000000)
		print(Code)
	end
	BEvent:Fire(Code)
	if Code ~= nil then
		Package:FireClient(Plr, Code)
	end
end)

then, my local script receives the code and does this:

Package.OnClientEvent:Connect(function(Pass)

AuthCode = Pass

end)

then, my local script calls a separate remote to preform the command. the local script passes through the name of the command and the code it got from the anticheat, and then the script that preforms the commands checks to see if it has the same code that was passed through from the remote, if it does it does the command, if not it does nothing. (yes, I know this is not a whitelist system, I am not done with that part.)

my problem is I have to add a wait(0.1) before I send the command and the code over to the script that preforms the commands, otherwise AuthCode equals nil and nothing works. here is my question: how can I remove the wait? I have gotten multiple people saying it feel a little unresponsive, is my anticheat script inefficient? do I need the wait? is there some better way to do this anticheat that I did not think of?

all help is welcome, thanks for taking the time to read this!

1 Like

I also realize I forgot to add a wait() into my for loop, I took it out to test if it was the problem. it was not.

1 Like

To start with replace any instance of “wait()” with “task.wait()” since “wait()” is now deprecated.

To fix any lag issues you may be noticing you should use “task.wait()” commands sparingly.

Try to replace “wait(0.1) with task.wait()” does it still work?

the problem is not that I use wait() or task.wait(), it is that I do not use it and it still has delay. the only spot I use it is in my local GUI script so that my GUI script can wait for the anticheat to catch up. I have 0 clue what is making the Anticheat script move slow.

Do you have any loops where it is used?

I had one, but I took wait out of it to test, it was relatively small so it did not crash, but it still is laggy.

You don’t really need the Auth as long as you check the player, an exploiter can easily get the Auth from the script and send it anyway, so its not really doing anything anticheat wise.

You should just have a table of all the players with perms to use it, and check if the player sending the remote is in the table, since exploiters can’t change the player sending the remote. Basically a whitelist system.

1 Like

I thought they could change the player sending the remote, thanks! As a backup I was gonna implement a system to check if the player who sent the remote is in the server, they can’t change who sent it so I do not need it right?

1 Like

Yep, the Player sending the remote can’t be altered, so you don’t need it.

1 Like