Why isn't this gui not stopping duplication of object

How would you break a loop to stop duplication of an object in ServerStorage?

What I have is a button that when you hold it down or just click it, the chickens start spawning and don’t stop even if you release your mouse from the button. Also, the chickens randomly stop and get stuck after some time.

My script:

local btn = script.Parent
local chicken = game.ServerStorage.Beans
btn.MouseButton1Down:Connect(function()
	local holding = true
	for i = 1,1000000000 do
		wait(0.5)
		chicken:Clone().Parent = game.Workspace
		if holding == false then
			break
		end
	end
	btn.MouseButton1Up:Connect(function()
		holding = false
	end)
end)

Also, the objects randomly stop and get stuck after some time.
robloxapp-20210823-2047392.wmv (1.6 MB)

1 Like

Move the holding variable out of the MouseButton1Down method… You should also move the MouseButton1Up method out of it too. Quick demonstration:

local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = true

btn.MouseButton1Down:Connect(function()	
	for i = 1,1000000000 do
		wait(0.5)
		chicken:Clone().Parent = game.Workspace
		if holding == false then
			break
		end
	end
end)

btn.MouseButton1Up:Connect(function()
    holding = false
end)

@ItsSovxy, this does not work, it break the loop, but doesn’t spawn a chicken every (0.5) seconds.

robloxapp-20210824-1654359.wmv (1.1 MB)

Why don’t you move the wait to the bottom of the loop instead of the start

instead of using a for function, instead use a while function.

local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = true

btn.MouseButton1Down:Connect(function()
       holding = true
	while holding do
		wait(0.5)
		chicken:Clone().Parent = game.Workspace
	end

	btn.MouseButton1Up:Connect(function()
		holding = false
	end)
end)

oops, wait

local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = false

btn.MouseButton1Down:Connect(function()
        holding = true
	while holding do
		wait(0.5)
		chicken:Clone().Parent = game.Workspace
		if not holding then
			break
		end
	end

	btn.MouseButton1Up:Connect(function()
		holding = false
	end)
end)

Yeah I saw that you missed that and I was about to say…

It works although the chickens still get compacted and stop moving after a couple of seconds. And you can stack up the spawning so that they spawn twice as fast or even maybe three-times as fast.

robloxapp-20210824-1714069.wmv (1.7 MB)

Ok, but what about them stacking up? And, 1 more problem, how would I prevent someone from using an auto clicker? I thought of putting a wait at the beginning of the mousedown function, but I don’t want them to have to wait for the chickens to spawn. PLEASE ANSWER!

@Threisth, I tried something like this but it didn’t work at all.

local btn = script.Parent
local chicken = game.ServerStorage.Beans
local holding = false
local canhold = true
btn.MouseButton1Down:Connect(function()
	holding = true
	while holding do
		if canhold == true then
			wait(0.5)
			chicken:Clone().Parent = game.Workspace
		end
		btn.MouseButton1Up:Connect(function()
			holding = false
			canhold = false
			wait(0.5)
			canhold = true
		end)
	end
end)