Autoclick detection

Hello everyone, I’m trying to make something that kicks player when their cps is over average human limits, but idk how to include it in this script.

I want to kick the player if their Left cps is over 50.

Heres the code:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cpsLabel_L = script.Parent:WaitForChild("LeftCPS")
local cpsLabel_R = script.Parent:WaitForChild("RightCPS")
local clicksL = 0
local clicksR = 0

mouse.Button1Down:Connect(function()
	clicksL = clicksL + 1
end)
mouse.Button2Down:Connect(function()
	clicksR = clicksR + 1
end)

while wait() do
	spawn(function()
			wait(1)
			cpsLabel_L.Text = "LeftMB_CPS: " .. clicksL
			clicksL = 0
	end)
		wait(1)
		cpsLabel_R.Text = "RightMB_CPS: " .. clicksR
		clicksR = 0
end

if clicksL >50 then
	plr:Kick("Autoclicking Prevention")
end

There is no really a reason to make client side autoclick prevention as it does not really gives an advantage to the exploiter, also exploiters will be able to bypass any check you implement on client-side, it’s just a matter of time.

If you want to implement something server-side secure, then you should learn how the client-server communication works in Roblox, you can find it here and in other related topics.

2 Likes

I understand, I’m trying to make this so people who don’t exploit, cannot manage to use autoclicking. Because if there were to be an exploiter, there aren’t really anything you can do when they bypass. Owning an autoclicker application would be more common than exploits.

1 Like

I think you’re close, just a bit overly complicated. We just need to loop every second, check if our clicks over time (deltatime should be close to 1) are greater than 50, and then kick if that is the case. (But yeah, trusting the client is a bigger issue altogether)

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cpsLabel_L = script.Parent:WaitForChild("LeftCPS")
local cpsLabel_R = script.Parent:WaitForChild("RightCPS")
local clicksL = 0
local clicksR = 0

mouse.Button1Down:Connect(function()
	clicksL = clicksL + 1
end)
mouse.Button2Down:Connect(function()
	clicksR = clicksR + 1
end)

while true do
	local deltatime = task.wait(1)
	if clicksL/deltatime > 50 then
		plr:Kick("Autoclicking Prevention")
	end
	cpsLabel_L.Text = "LeftMB_CPS: " .. clicksL
	cpsLabel_R.Text = "RightMB_CPS: " .. clicksR
	clicksL = 0
	clicksR = 0
end
3 Likes

You shouldn’t worry about what inputs the user is sending. Worrying about what effect they’re having on the game. Does an auto clicker let them instantly kill other players? Detect that directly instead of tunnel visioning on the specific interface they happen to use.

2 Likes

This should work:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cpsLabel_L = script.Parent:WaitForChild("LeftCPS")
local cpsLabel_R = script.Parent:WaitForChild("RightCPS")
local clicksL = 0
local clicksR = 0

local MAXIMUM_AMOUNT_OF_CPS = 50

mouse.Button1Down:Connect(function()
	clicksL += 1
	cpsLabel_L.Text = "LeftMB_CPS: " .. clicksL
end)

mouse.Button2Down:Connect(function()
	clicksR += 1
	cpsLabel_R.Text = "RightMB_CPS: " .. clicksR
end)

task.spawn(function()
	-- this thread manages cps reset
	while true do
		task.wait(1)
		clicksL = 0
		clicksR = 0
      cpsLabel_L.Text = "LeftMB_CPS: 0"
      cpsLabel_R.Text = "RightMB_CPS: 0"
	end
end)

task.spawn(function()
	-- this thread manages the autoclicker
	while true do
		task.wait()
		if clicksL > MAXIMUM_AMOUNT_OF_CPS or clicksR > MAXIMUM_AMOUNT_OF_CPS then
			plr:Kick("Autoclicking Prevention")
		end
	end
end)
1 Like

This game I am attempting to create is nearly all about cps, although I do not wish players to use unfair advantages such as autoclicking, and exploiting.

Thank you for this, however if I use this method, the GUI it is updating to may not work.

Thank you, sorry if its too much but, I would like to learn a bit more about this deltatime function.

wait and task.wait both return a number which is the time it takes for the wait to actually happen. If for some reason task.wait(1) takes 1.1 seconds to run, we shouldn’t punish players for getting 50 clicks in, since that’s no longer actually 50 cps, it’s 50 clicks / 1.1 seconds or 45.5 cps. That’s all I’m doing by getting a deltatime. Hopefully that makes sense.

3 Likes