Anti lag script making things worse

I am trying to add a script that adds stuff to the workspace. I’m also trying to make it, so people can’t lag the game using an auto clicker, when I click it, it makes more than it should of the part. the script is:

local bread = game.ServerStorage.Batter
while wait(1) do
script.Parent.ClickDetector.MouseClick:Connect(function()
	local batter = bread:Clone()
	batter.Parent = game.Workspace
	end)
	end

You can use debounce for solving this problem:

local bread = game.ServerStorage.Batter
local canSpawn = true

script.Parent.ClickDetector.MouseClick:Connect(function()
    if canSpawn then
        canSpawn = false
       
        local batter = bread:Clone()
	    batter.Parent = game.Workspace
        wait(1)
        canSpawn = true
    end
	
end)
1 Like

Your code is making a new connection every second, meaning it’s going to do what you mentioned, effectively making it a lag script more than an anti lag, just do a debounce like @Infi_power mentioned, although i would recommend making it like this instead

local bread = game.ServerStorage.Batter
local canSpawn = true

script.Parent.ClickDetector.MouseClick:Connect(function()
    if not canSpawn then return end
	canSpawn = false
	bread:Clone().Parent = workspace
	wait(1)
	canSpawn = true
end)

Simpler way to go about it, I did a guard clause to remove some indentation that isn’t really needed and just directly referenced a clone than making it into a varaible since it’s only used for 1 thing only, parenting it

2 Likes