How to break out of button1down:connect(function) loop

I want to place an object down once then stop the click, I tired to return to break anything else I can do?
I tried a debounce but that just made it fire more than once at the same time, so if I clicked once then it would fire twice, ect.

Code:

local Functions = game.ReplicatedStorage.Functions
local Place = Functions.Place
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local UIS = game:GetService("UserInputService")
local Buttons = script.Parent:GetChildren()



local function PlaceItem(Button)
	Mouse.Button1Down:Connect(function()
		local Positions = Mouse.Hit.p
		Place:InvokeServer(Positions, Button.Tower.Value)
		return	
	end)
	return
end

for Index, Button in ipairs(Buttons) do
	if Button:IsA("ImageButton") then
		Button.MouseButton1Click:Connect(function()
			PlaceItem(Button)
		end)
	end
end

Your two best options are:

  1. Define the event outside the function. Have a debounce-style check for a variable called isPlacing which you set to true in your PlaceItem function and set it to false once the click has occurred, inside the event callback.

  2. Disconnect the event callback after the click has occurred by storing the RbxScriptConnection in a variable, and calling :Disconnect() on the variable inside the event callback.

1 Like

Thanks for help, means a lot. :smiley: