How do you disconnect an event from inside itself?

I’m trying to have my button disconnect after its being clicked - but PlaceButton:Disconnect() says that it is unknown globally. How would I go about disconnecting it?

		local PlaceButton = UIS.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				if PlacingStructure == true and goodToPlace == true then
					local StructureCFrame = clientStructure.PrimaryPart.CFrame
					PlacedStructure = PlaceStructure:InvokeServer(clientStructure.Name, StructureCFrame)
					PlacingItem = nil
					if PlacedStructure == true then
						PlacingStructure = false
						clientStructure:Destroy()
						Render:Disconnect()
						Rotates:Disconnect()
						Rotate:Disconnect()
						PlaceButton:Disconnect()
					end
				end
			end			
		end)

Try this:

local PlaceButton
PlaceButton = UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if PlacingStructure == true and goodToPlace == true then
			local StructureCFrame = clientStructure.PrimaryPart.CFrame
			PlacedStructure = PlaceStructure:InvokeServer(clientStructure.Name, StructureCFrame)
			PlacingItem = nil
			if PlacedStructure == true then
				PlacingStructure = false
				clientStructure:Destroy()
				Render:Disconnect()
				Rotates:Disconnect()
				Rotate:Disconnect()
				PlaceButton:Disconnect()
			end
		end
	end			
end)

Thanks it works! Why does it behave that way?

It must be because the thread inside the event cannot access the variable. To be able to access the variable it has to be in the same thread.

1 Like