When I call the function with via mouse, it creates multiple clones instead of one clone, how to fix this?


--Variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local goodfarm = game.ServerStorage.GoodFarm
local badfarm = game.ServerStorage.BadFarm
local Farm = game.ServerStorage.Farm
local Debounce = false
local found = game.ServerStorage:FindFirstChild("GoodFarm")
local found2 = game.ServerStorage:FindFirstChild("BadFarm")
local ClonedFarm = nil
--Move Part to Mouse Position Always
while true do
	print(mouse.Target)
	
	--Good
	if mouse.Target == game.Workspace.Island then
		mouse.TargetFilter = goodfarm
		badfarm.Parent = game.ServerStorage
		
		if found then
			goodfarm.Parent = workspace
		end
		goodfarm.Position = mouse.Hit.p
--Problem  |
--		  \/
		mouse.Button1Down:connect(function()
			Debounce = true
			Farm:Clone().Parent = workspace
			return
		end)
		
	end



	--Bad
	if mouse.Target == game.Workspace.Water then
		goodfarm.Parent = game.ServerStorage
		mouse.TargetFilter = badfarm
		
		if found2 then
			badfarm.Parent = workspace
		end
		
		badfarm.Position = mouse.Hit.p
	end
	
	if Debounce == true then
		goodfarm.Parent = game.ServerStorage
		badfarm.Parent = game.ServerStorage
		break
	end
	wait()
end


3 comments

0

You are making endless connections with that loop and the function of mouse.Button1Down, you need to only create one event

0

Yeah, try MouseButton1Click

0

You put the function in the loop, so it's creating multiple listeners every time the loop repeats. <.> To fix this problem, just simply put the event/function outside the loop. :)